CRM-15988 - Cleanup internal use of entity names
[civicrm-core.git] / Civi / API / Provider / ReflectionProvider.php
index 676458faea696201e9196db6ec19609ea24dd0c1..bd56c65ddd7c97d43836901fcfb9b61ee7b23c5b 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 namespace Civi\API\Provider;
+
 use Civi\API\Events;
 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
@@ -39,10 +40,12 @@ class ReflectionProvider implements EventSubscriberInterface, ProviderInterface
   public static function getSubscribedEvents() {
     return array(
       Events::RESOLVE => array(
-        array('onApiResolve', Events::W_EARLY), // TODO decide if we really want to override others
+        // TODO decide if we really want to override others
+        array('onApiResolve', Events::W_EARLY),
       ),
       Events::AUTHORIZE => array(
-        array('onApiAuthorize', Events::W_EARLY), // TODO decide if we really want to override others
+        // TODO decide if we really want to override others
+        array('onApiAuthorize', Events::W_EARLY),
       ),
     );
   }
@@ -59,6 +62,7 @@ class ReflectionProvider implements EventSubscriberInterface, ProviderInterface
 
   /**
    * @param \Civi\API\Kernel $apiKernel
+   *   The API kernel.
    */
   public function __construct($apiKernel) {
     $this->apiKernel = $apiKernel;
@@ -70,25 +74,29 @@ class ReflectionProvider implements EventSubscriberInterface, ProviderInterface
 
   /**
    * @param \Civi\API\Event\ResolveEvent $event
+   *   API resolution event.
    */
   public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
     $apiRequest = $event->getApiRequest();
-    $actions = isset($this->actions[$apiRequest['entity']]) ? $this->actions[$apiRequest['entity']] : $this->actions['*'];
+    $actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
     if (in_array($apiRequest['action'], $actions)) {
       $apiRequest['is_metadata'] = TRUE;
       $event->setApiRequest($apiRequest);
       $event->setApiProvider($this);
-      $event->stopPropagation(); // TODO decide if we really want to override others
+      $event->stopPropagation();
+      // TODO decide if we really want to override others
     }
   }
 
   /**
    * @param \Civi\API\Event\AuthorizeEvent $event
+   *   API authorization event.
    */
   public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
     $apiRequest = $event->getApiRequest();
     if (isset($apiRequest['is_metadata'])) {
-      // if (\CRM_Core_Permission::check('access AJAX API') || \CRM_Core_Permission::check('access CiviCRM')) {
+      // if (\CRM_Core_Permission::check('access AJAX API')
+      //   || \CRM_Core_Permission::check('access CiviCRM')) {
       $event->authorize();
       $event->stopPropagation();
       // }
@@ -97,34 +105,46 @@ class ReflectionProvider implements EventSubscriberInterface, ProviderInterface
 
   /**
    * {inheritdoc}
+   * @param array $apiRequest
+   * @return array
+   * @throws \API_Exception
    */
   public function invoke($apiRequest) {
-    if ($apiRequest['entity'] == 'Entity' && $apiRequest['action'] == 'get') {
-      return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']));
+    if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
+      return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
     }
     switch ($apiRequest['action']) {
       case 'getactions':
-        return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']));
-//      case 'getfields':
-//        return $this->doGetFields($apiRequest);
+        return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
+
+      //case 'getfields':
+      //  return $this->doGetFields($apiRequest);
+
       default:
     }
 
     // We shouldn't get here because onApiResolve() checks $this->actions
-    throw new \API_Exception("Unsupported action ("  . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
+    throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
   }
 
   /**
    * {inheritdoc}
+   * @param int $version
+   * @return array
    */
-  function getEntityNames($version) {
+  public function getEntityNames($version) {
     return array('Entity');
   }
 
   /**
    * {inheritdoc}
+   * @param int $version
+   * @param string $entity
+   * @return array
    */
-  function getActionNames($version, $entity) {
+  public function getActionNames($version, $entity) {
+    $entity = _civicrm_api_get_camel_name($entity, $version);
     return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*'];
   }
+
 }