Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-19-00-06-22
[civicrm-core.git] / Civi / API / Provider / AdhocProvider.php
CommitLineData
70265090
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26*/
27
28namespace Civi\API\Provider;
29use Civi\API\Events;
30use Symfony\Component\EventDispatcher\EventSubscriberInterface;
31
32/**
33 * An adhoc provider is useful for creating mock API implementations.
34 */
35class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
36
6550386a
EM
37 /**
38 * @return array
39 */
70265090 40 public static function getSubscribedEvents() {
db8b4473
TO
41 // Using a high priority allows adhoc implementations
42 // to override standard implementations -- which is
43 // handy for testing/mocking.
70265090
TO
44 return array(
45 Events::RESOLVE => array(
db8b4473 46 array('onApiResolve', Events::W_EARLY),
70265090
TO
47 ),
48 Events::AUTHORIZE => array(
db8b4473 49 array('onApiAuthorize', Events::W_EARLY),
70265090
TO
50 ),
51 );
52 }
53
54 /**
55 * @var array (string $name => array('perm' => string, 'callback' => callable))
56 */
57 private $actions = array();
58
59 /**
60 * @var string
61 */
62 private $entity;
63
64 /**
65 * @var int
66 */
67 private $version;
68
69 /**
70 * @param int $version
71 * @param string $entity
72 */
73 public function __construct($version, $entity) {
74 $this->entity = $entity;
75 $this->version = $version;
76 }
77
78 /**
79 * @param string $name
80 * @param string $perm
81 * @param callable $callback
82 * @return ReflectionProvider
83 */
84 public function addAction($name, $perm, $callback) {
85 $this->actions[strtolower($name)] = array(
86 'perm' => $perm,
87 'callback' => $callback,
88 );
89 return $this;
90 }
91
6550386a
EM
92 /**
93 * @param \Civi\API\Event\ResolveEvent $event
94 */
70265090
TO
95 public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
96 $apiRequest = $event->getApiRequest();
97 if ($this->matchesRequest($apiRequest)) {
98 $event->setApiRequest($apiRequest);
99 $event->setApiProvider($this);
100 $event->stopPropagation();
101 }
102 }
103
6550386a
EM
104 /**
105 * @param \Civi\API\Event\AuthorizeEvent $event
106 */
70265090
TO
107 public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
108 $apiRequest = $event->getApiRequest();
109 if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
110 $event->authorize();
111 $event->stopPropagation();
112 }
113 }
114
115 /**
116 * {inheritdoc}
117 */
118 public function invoke($apiRequest) {
119 return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
120 }
121
122 /**
123 * {inheritdoc}
124 */
125 function getEntityNames($version) {
126 return array($this->entity);
127 }
128
129 /**
130 * {inheritdoc}
131 */
132 function getActionNames($version, $entity) {
133 if ($version == $this->version && $entity == $this->entity) {
134 return array_keys($this->actions);
135 }
136 else {
137 return array();
138 }
139 }
140
6550386a
EM
141 /**
142 * @param $apiRequest
143 *
144 * @return bool
145 */
70265090
TO
146 public function matchesRequest($apiRequest) {
147 return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
148 }
6550386a 149}