Merge pull request #15734 from seamuslee001/remove_activity_option_join_custom_search
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 namespace Civi\API\Provider;
13
14 use Civi\API\Events;
15 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17 /**
18 * An adhoc provider is useful for creating mock API implementations.
19 */
20 class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
21
22 /**
23 * @return array
24 */
25 public static function getSubscribedEvents() {
26 // Using a high priority allows adhoc implementations
27 // to override standard implementations -- which is
28 // handy for testing/mocking.
29 return [
30 Events::RESOLVE => [
31 ['onApiResolve', Events::W_EARLY],
32 ],
33 Events::AUTHORIZE => [
34 ['onApiAuthorize', Events::W_EARLY],
35 ],
36 ];
37 }
38
39 /**
40 * @var array (string $name => array('perm' => string, 'callback' => callable))
41 */
42 protected $actions = [];
43
44 /**
45 * @var string
46 */
47 protected $entity;
48
49 /**
50 * @var int
51 */
52 protected $version;
53
54 /**
55 * @param int $version
56 * API version.
57 * @param string $entity
58 * API entity.
59 */
60 public function __construct($version, $entity) {
61 $this->entity = $entity;
62 $this->version = $version;
63 }
64
65 /**
66 * Register a new API.
67 *
68 * @param string $name
69 * Action name.
70 * @param string $perm
71 * Permissions required for invoking the action.
72 * @param mixed $callback
73 * The function which executes the API.
74 * @return AdhocProvider
75 */
76 public function addAction($name, $perm, $callback) {
77 $this->actions[strtolower($name)] = [
78 'perm' => $perm,
79 'callback' => $callback,
80 ];
81 return $this;
82 }
83
84 /**
85 * @param \Civi\API\Event\ResolveEvent $event
86 * API resolution event.
87 */
88 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
89 $apiRequest = $event->getApiRequest();
90 if ($this->matchesRequest($apiRequest)) {
91 $event->setApiRequest($apiRequest);
92 $event->setApiProvider($this);
93 $event->stopPropagation();
94 }
95 }
96
97 /**
98 * @param \Civi\API\Event\AuthorizeEvent $event
99 * API authorization event.
100 */
101 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
102 $apiRequest = $event->getApiRequest();
103 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
104 $event->authorize();
105 $event->stopPropagation();
106 }
107 }
108
109 /**
110 * @inheritDoc
111 * @param array $apiRequest
112 * @return array|mixed
113 */
114 public function invoke($apiRequest) {
115 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
116 }
117
118 /**
119 * @inheritDoc
120 * @param int $version
121 * @return array
122 */
123 public function getEntityNames($version) {
124 return [$this->entity];
125 }
126
127 /**
128 * @inheritDoc
129 * @param int $version
130 * @param string $entity
131 * @return array
132 */
133 public function getActionNames($version, $entity) {
134 if ($version == $this->version && $entity == $this->entity) {
135 return array_keys($this->actions);
136 }
137 else {
138 return [];
139 }
140 }
141
142 /**
143 * @param array $apiRequest
144 * The full description of the API request.
145 *
146 * @return bool
147 */
148 public function matchesRequest($apiRequest) {
149 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
150 }
151
152 }