Merge pull request #15523 from jamie-tillman/patch-1
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
CommitLineData
70265090
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
41498ac5 4 | Copyright CiviCRM LLC. All rights reserved. |
70265090 5 | |
41498ac5
TO
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 |
70265090 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
70265090
TO
11
12namespace Civi\API\Provider;
46bcf597 13
70265090
TO
14use Civi\API\Events;
15use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17/**
18 * An adhoc provider is useful for creating mock API implementations.
19 */
20class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
21
6550386a
EM
22 /**
23 * @return array
24 */
70265090 25 public static function getSubscribedEvents() {
db8b4473
TO
26 // Using a high priority allows adhoc implementations
27 // to override standard implementations -- which is
28 // handy for testing/mocking.
c64f69d9
CW
29 return [
30 Events::RESOLVE => [
31 ['onApiResolve', Events::W_EARLY],
32 ],
33 Events::AUTHORIZE => [
34 ['onApiAuthorize', Events::W_EARLY],
35 ],
36 ];
70265090
TO
37 }
38
39 /**
40 * @var array (string $name => array('perm' => string, 'callback' => callable))
41 */
c64f69d9 42 protected $actions = [];
70265090
TO
43
44 /**
45 * @var string
46 */
17b9f7be 47 protected $entity;
70265090
TO
48
49 /**
50 * @var int
51 */
17b9f7be 52 protected $version;
70265090
TO
53
54 /**
55 * @param int $version
8882ff5c 56 * API version.
70265090 57 * @param string $entity
8882ff5c 58 * API entity.
70265090
TO
59 */
60 public function __construct($version, $entity) {
61 $this->entity = $entity;
62 $this->version = $version;
63 }
64
65 /**
8882ff5c
TO
66 * Register a new API.
67 *
70265090 68 * @param string $name
8882ff5c 69 * Action name.
70265090 70 * @param string $perm
8882ff5c
TO
71 * Permissions required for invoking the action.
72 * @param mixed $callback
73 * The function which executes the API.
066c4638 74 * @return AdhocProvider
70265090
TO
75 */
76 public function addAction($name, $perm, $callback) {
c64f69d9 77 $this->actions[strtolower($name)] = [
70265090
TO
78 'perm' => $perm,
79 'callback' => $callback,
c64f69d9 80 ];
70265090
TO
81 return $this;
82 }
83
6550386a
EM
84 /**
85 * @param \Civi\API\Event\ResolveEvent $event
8882ff5c 86 * API resolution event.
6550386a 87 */
70265090
TO
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
6550386a
EM
97 /**
98 * @param \Civi\API\Event\AuthorizeEvent $event
8882ff5c 99 * API authorization event.
6550386a 100 */
70265090
TO
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 /**
3dde2c6c 110 * @inheritDoc
257e7666
EM
111 * @param array $apiRequest
112 * @return array|mixed
70265090
TO
113 */
114 public function invoke($apiRequest) {
115 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
116 }
117
118 /**
3dde2c6c 119 * @inheritDoc
257e7666
EM
120 * @param int $version
121 * @return array
70265090 122 */
8882ff5c 123 public function getEntityNames($version) {
c64f69d9 124 return [$this->entity];
70265090
TO
125 }
126
127 /**
3dde2c6c 128 * @inheritDoc
257e7666
EM
129 * @param int $version
130 * @param string $entity
131 * @return array
70265090 132 */
8882ff5c 133 public function getActionNames($version, $entity) {
70265090
TO
134 if ($version == $this->version && $entity == $this->entity) {
135 return array_keys($this->actions);
136 }
137 else {
c64f69d9 138 return [];
70265090
TO
139 }
140 }
141
6550386a 142 /**
8882ff5c
TO
143 * @param array $apiRequest
144 * The full description of the API request.
6550386a
EM
145 *
146 * @return bool
147 */
70265090
TO
148 public function matchesRequest($apiRequest) {
149 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
150 }
96025800 151
6550386a 152}