Civi/API - Code style
[civicrm-core.git] / Civi / API / Subscriber / TransactionSubscriber.php
CommitLineData
b55bc593
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
b55bc593 5 +--------------------------------------------------------------------+
39de6fd5 6 | Copyright CiviCRM LLC (c) 2004-2014 |
b55bc593
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 +--------------------------------------------------------------------+
26*/
27
28namespace Civi\API\Subscriber;
8882ff5c 29
b55bc593
TO
30use Civi\API\Events;
31use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
6550386a
EM
33/**
34 * Class TransactionSubscriber
e29a1b03
TO
35 *
36 * Implement transaction management for API calls. Two API options are accepted:
8882ff5c
TO
37 * - is_transactional: bool|'nest' - if true, then all work is done inside a
38 * transaction. By default, true for mutator actions (C-UD). 'nest' will
39 * force creation of a nested transaction; otherwise, the default is to
40 * re-use any existing transactions.
41 * - options.force_rollback: bool - if true, all work is done in a nested
42 * transaction which will be rolled back.
e29a1b03 43 *
6550386a
EM
44 * @package Civi\API\Subscriber
45 */
b55bc593 46class TransactionSubscriber implements EventSubscriberInterface {
6550386a
EM
47 /**
48 * @return array
49 */
b55bc593
TO
50 public static function getSubscribedEvents() {
51 return array(
52 Events::PREPARE => array('onApiPrepare', Events::W_EARLY),
53 Events::RESPOND => array('onApiRespond', Events::W_MIDDLE),
54 Events::EXCEPTION => array('onApiException', Events::W_EARLY),
55 );
56 }
57
58 /**
5558f278 59 * @var array (scalar $apiRequestId => CRM_Core_Transaction $tx)
b55bc593 60 */
5558f278
TO
61 private $transactions = array();
62
e29a1b03
TO
63 /**
64 * @var array (scalar $apiRequestId => bool)
65 *
66 * A list of requests which should be forcibly rolled back to
67 * their save points.
68 */
69 private $forceRollback = array();
70
5558f278
TO
71 /**
72 * Determine if an API request should be treated as transactional
73 *
74 * @param \Civi\API\Provider\ProviderInterface $apiProvider
8882ff5c 75 * The API provider responsible for this request.
5558f278 76 * @param array $apiRequest
8882ff5c 77 * The full API request.
5558f278
TO
78 * @return bool
79 */
80 public function isTransactional($apiProvider, $apiRequest) {
e29a1b03 81 if ($this->isForceRollback($apiProvider, $apiRequest)) {
8882ff5c 82 return TRUE;
e29a1b03 83 }
289c8dd4 84 if (isset($apiRequest['params']['is_transactional'])) {
e29a1b03 85 return \CRM_Utils_String::strtobool($apiRequest['params']['is_transactional']) || $apiRequest['params']['is_transactional'] == 'nest';
289c8dd4 86 }
5558f278
TO
87 return strtolower($apiRequest['action']) == 'create' || strtolower($apiRequest['action']) == 'delete' || strtolower($apiRequest['action']) == 'submit';
88 }
b55bc593 89
e29a1b03
TO
90 /**
91 * Determine if caller wants us to *always* rollback.
92 *
93 * @param \Civi\API\Provider\ProviderInterface $apiProvider
8882ff5c 94 * The API provider responsible for this request.
e29a1b03 95 * @param array $apiRequest
8882ff5c 96 * The full API request.
e29a1b03
TO
97 * @return bool
98 */
99 public function isForceRollback($apiProvider, $apiRequest) {
8882ff5c 100 // FIXME: When APIv3 uses better parsing, only one check will be needed.
e29a1b03
TO
101 if (isset($apiRequest['params']['options']['force_rollback'])) {
102 return \CRM_Utils_String::strtobool($apiRequest['params']['options']['force_rollback']);
103 }
104 if (isset($apiRequest['options']['force_rollback'])) {
105 return \CRM_Utils_String::strtobool($apiRequest['options']['force_rollback']);
106 }
107 return FALSE;
108 }
109
110 /**
111 * Determine if caller wants a nested transaction or a re-used transaction.
112 *
113 * @param \Civi\API\Provider\ProviderInterface $apiProvider
8882ff5c 114 * The API provider responsible for this request.
e29a1b03 115 * @param array $apiRequest
8882ff5c 116 * The full API request.
e29a1b03
TO
117 * @return bool True if a new nested transaction is required; false if active tx may be used
118 */
119 public function isNested($apiProvider, $apiRequest) {
120 if ($this->isForceRollback($apiProvider, $apiRequest)) {
121 return TRUE;
122 }
8882ff5c 123 if (isset($apiRequest['params']['is_transactional']) && $apiRequest['params']['is_transactional'] === 'nest') {
e29a1b03
TO
124 return TRUE;
125 }
126 return FALSE;
127 }
128
b55bc593
TO
129 /**
130 * Open a new transaction instance (if appropriate in the current policy)
131 *
5558f278 132 * @param \Civi\API\Event\PrepareEvent $event
8882ff5c 133 * API preparation event.
b55bc593 134 */
8882ff5c 135 public function onApiPrepare(\Civi\API\Event\PrepareEvent $event) {
b55bc593 136 $apiRequest = $event->getApiRequest();
5558f278 137 if ($this->isTransactional($event->getApiProvider(), $apiRequest)) {
e29a1b03
TO
138 $this->transactions[$apiRequest['id']] = new \CRM_Core_Transaction($this->isNested($event->getApiProvider(), $apiRequest));
139 }
140 if ($this->isForceRollback($event->getApiProvider(), $apiRequest)) {
141 $this->transactions[$apiRequest['id']]->rollback();
b55bc593
TO
142 }
143 }
144
145 /**
146 * Close any pending transactions
8882ff5c
TO
147 *
148 * @param \Civi\API\Event\RespondEvent $event
149 * API response event.
b55bc593 150 */
8882ff5c 151 public function onApiRespond(\Civi\API\Event\RespondEvent $event) {
5558f278 152 $apiRequest = $event->getApiRequest();
ea24cf7f
TO
153 if (isset($this->transactions[$apiRequest['id']])) {
154 if (civicrm_error($event->getResponse())) {
155 $this->transactions[$apiRequest['id']]->rollback();
156 }
157 unset($this->transactions[$apiRequest['id']]);
158 }
b55bc593
TO
159 }
160
161 /**
162 * Rollback the pending transaction
163 *
164 * @param \Civi\API\Event\ExceptionEvent $event
8882ff5c 165 * API exception event.
b55bc593 166 */
8882ff5c 167 public function onApiException(\Civi\API\Event\ExceptionEvent $event) {
5558f278
TO
168 $apiRequest = $event->getApiRequest();
169 if (isset($this->transactions[$apiRequest['id']])) {
170 $this->transactions[$apiRequest['id']]->rollback();
171 unset($this->transactions[$apiRequest['id']]);
b55bc593
TO
172 }
173 }
6550386a 174}