Merge pull request #23015 from ginkgomzd/php8-unsupported-operand
[civicrm-core.git] / Civi / Api4 / Generic / BasicBatchAction.php
CommitLineData
19b53e5b
C
1<?php
2
380f3545
TO
3/*
4 +--------------------------------------------------------------------+
41498ac5 5 | Copyright CiviCRM LLC. All rights reserved. |
380f3545 6 | |
41498ac5
TO
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
380f3545
TO
10 +--------------------------------------------------------------------+
11 */
12
19b53e5b
C
13namespace Civi\Api4\Generic;
14
15use Civi\API\Exception\NotImplementedException;
929a9585
CW
16use Civi\API\Exception\UnauthorizedException;
17use Civi\Api4\Utils\CoreUtil;
19b53e5b
C
18
19/**
e3c6d5ff 20 * $ACTION one or more $ENTITIES.
19b53e5b 21 *
e3c6d5ff 22 * $ENTITIES are selected based on criteria specified in `where` parameter (required).
19b53e5b
C
23 *
24 * @package Civi\Api4\Generic
25 */
26class BasicBatchAction extends AbstractBatchAction {
27
28 /**
29 * @var callable
9bafff7c 30 * Function(array $item, BasicBatchAction $thisAction): array
19b53e5b
C
31 */
32 private $doer;
33
34 /**
35 * BasicBatchAction constructor.
36 *
fc95d9a5
CW
37 * ```php
38 * $myAction = new BasicBatchAction($entityName, $actionName, function($item) {
39 * // Do something with $item
40 * $return $item;
41 * });
42 * ```
43 *
19b53e5b
C
44 * @param string $entityName
45 * @param string $actionName
19b53e5b 46 * @param callable $doer
19b53e5b 47 */
29ab318b
CW
48 public function __construct($entityName, $actionName, $doer = NULL) {
49 parent::__construct($entityName, $actionName);
19b53e5b 50 $this->doer = $doer;
29ab318b
CW
51 // Accept doer as 4th param for now, but emit deprecated warning
52 $this->doer = func_get_args()[3] ?? NULL;
53 if ($this->doer) {
54 \CRM_Core_Error::deprecatedWarning(__CLASS__ . ' constructor received $doer as 4th param; it should be the 3rd as the $select param has been removed');
55 }
56 else {
57 $this->doer = $doer;
58 }
19b53e5b
C
59 }
60
61 /**
cc9b2a95
CW
62 * Checks permissions and then delegates to processBatch.
63 *
64 * Note: Unconditional logic must go here in the run function, as delegated functions may be overridden.
19b53e5b
C
65 *
66 * @param \Civi\Api4\Generic\Result $result
67 */
68 public function _run(Result $result) {
cc9b2a95
CW
69 $items = $this->getBatchRecords();
70 foreach ($items as $item) {
70da3927 71 if ($this->checkPermissions && !CoreUtil::checkAccessRecord($this, $item, \CRM_Core_Session::getLoggedInContactID() ?: 0)) {
929a9585
CW
72 throw new UnauthorizedException("ACL check failed");
73 }
cc9b2a95
CW
74 }
75 $this->processBatch($result, $items);
76 }
77
78 /**
79 * Calls doTask once per item and stores the result.
80 *
81 * We pass the doTask function an array representing one item to process.
82 * We expect to get the same format back.
83 *
84 * Note: This function may be overridden by the end api.
85 *
86 * @param Result $result
87 * @param array $items
88 * @throws NotImplementedException
89 */
90 protected function processBatch(Result $result, array $items) {
91 foreach ($items as $item) {
19b53e5b
C
92 $result[] = $this->doTask($item);
93 }
94 }
95
96 /**
97 * This Basic Batch class can be used in one of two ways:
98 *
99 * 1. Use this class directly by passing a callable ($doer) to the constructor.
100 * 2. Extend this class and override this function.
101 *
102 * Either way, this function should return an array with an output record
103 * for the item.
104 *
105 * @param array $item
106 * @return array
107 * @throws \Civi\API\Exception\NotImplementedException
108 */
109 protected function doTask($item) {
110 if (is_callable($this->doer)) {
32f72d83 111 $this->addCallbackToDebugOutput($this->doer);
19b53e5b
C
112 return call_user_func($this->doer, $item, $this);
113 }
114 throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName());
115 }
116
117}