(NFC) (dev/core#878) Simplify copyright header template
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
CommitLineData
70265090
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
70265090 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
70265090
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
70265090
TO
27
28namespace Civi\API\Provider;
46bcf597 29
70265090
TO
30use Civi\API\Events;
31use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33/**
34 * An adhoc provider is useful for creating mock API implementations.
35 */
36class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
37
6550386a
EM
38 /**
39 * @return array
40 */
70265090 41 public static function getSubscribedEvents() {
db8b4473
TO
42 // Using a high priority allows adhoc implementations
43 // to override standard implementations -- which is
44 // handy for testing/mocking.
c64f69d9
CW
45 return [
46 Events::RESOLVE => [
47 ['onApiResolve', Events::W_EARLY],
48 ],
49 Events::AUTHORIZE => [
50 ['onApiAuthorize', Events::W_EARLY],
51 ],
52 ];
70265090
TO
53 }
54
55 /**
56 * @var array (string $name => array('perm' => string, 'callback' => callable))
57 */
c64f69d9 58 protected $actions = [];
70265090
TO
59
60 /**
61 * @var string
62 */
17b9f7be 63 protected $entity;
70265090
TO
64
65 /**
66 * @var int
67 */
17b9f7be 68 protected $version;
70265090
TO
69
70 /**
71 * @param int $version
8882ff5c 72 * API version.
70265090 73 * @param string $entity
8882ff5c 74 * API entity.
70265090
TO
75 */
76 public function __construct($version, $entity) {
77 $this->entity = $entity;
78 $this->version = $version;
79 }
80
81 /**
8882ff5c
TO
82 * Register a new API.
83 *
70265090 84 * @param string $name
8882ff5c 85 * Action name.
70265090 86 * @param string $perm
8882ff5c
TO
87 * Permissions required for invoking the action.
88 * @param mixed $callback
89 * The function which executes the API.
066c4638 90 * @return AdhocProvider
70265090
TO
91 */
92 public function addAction($name, $perm, $callback) {
c64f69d9 93 $this->actions[strtolower($name)] = [
70265090
TO
94 'perm' => $perm,
95 'callback' => $callback,
c64f69d9 96 ];
70265090
TO
97 return $this;
98 }
99
6550386a
EM
100 /**
101 * @param \Civi\API\Event\ResolveEvent $event
8882ff5c 102 * API resolution event.
6550386a 103 */
70265090
TO
104 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
105 $apiRequest = $event->getApiRequest();
106 if ($this->matchesRequest($apiRequest)) {
107 $event->setApiRequest($apiRequest);
108 $event->setApiProvider($this);
109 $event->stopPropagation();
110 }
111 }
112
6550386a
EM
113 /**
114 * @param \Civi\API\Event\AuthorizeEvent $event
8882ff5c 115 * API authorization event.
6550386a 116 */
70265090
TO
117 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
118 $apiRequest = $event->getApiRequest();
119 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
120 $event->authorize();
121 $event->stopPropagation();
122 }
123 }
124
125 /**
3dde2c6c 126 * @inheritDoc
257e7666
EM
127 * @param array $apiRequest
128 * @return array|mixed
70265090
TO
129 */
130 public function invoke($apiRequest) {
131 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
132 }
133
134 /**
3dde2c6c 135 * @inheritDoc
257e7666
EM
136 * @param int $version
137 * @return array
70265090 138 */
8882ff5c 139 public function getEntityNames($version) {
c64f69d9 140 return [$this->entity];
70265090
TO
141 }
142
143 /**
3dde2c6c 144 * @inheritDoc
257e7666
EM
145 * @param int $version
146 * @param string $entity
147 * @return array
70265090 148 */
8882ff5c 149 public function getActionNames($version, $entity) {
70265090
TO
150 if ($version == $this->version && $entity == $this->entity) {
151 return array_keys($this->actions);
152 }
153 else {
c64f69d9 154 return [];
70265090
TO
155 }
156 }
157
6550386a 158 /**
8882ff5c
TO
159 * @param array $apiRequest
160 * The full description of the API request.
6550386a
EM
161 *
162 * @return bool
163 */
70265090
TO
164 public function matchesRequest($apiRequest) {
165 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
166 }
96025800 167
6550386a 168}