Commit | Line | Data |
---|---|---|
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 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | ||
21 | ||
19b53e5b C |
22 | namespace Civi\Api4\Generic; |
23 | ||
24 | use Civi\API\Exception\NotImplementedException; | |
25 | ||
26 | /** | |
27 | * Basic action for deleting or performing some other task with a set of records. Ex: | |
28 | * | |
29 | * $myAction = new BasicBatchAction('Entity', 'action', function($item) { | |
30 | * // Do something with $item | |
31 | * $return $item; | |
32 | * }); | |
33 | * | |
34 | * @package Civi\Api4\Generic | |
35 | */ | |
36 | class BasicBatchAction extends AbstractBatchAction { | |
37 | ||
38 | /** | |
39 | * @var callable | |
40 | * | |
41 | * Function(array $item, BasicBatchAction $thisAction) => array | |
42 | */ | |
43 | private $doer; | |
44 | ||
45 | /** | |
46 | * BasicBatchAction constructor. | |
47 | * | |
48 | * @param string $entityName | |
49 | * @param string $actionName | |
50 | * @param string|array $select | |
51 | * One or more fields to select from each matching item. | |
52 | * @param callable $doer | |
53 | * Function(array $item, BasicBatchAction $thisAction) => array | |
54 | */ | |
55 | public function __construct($entityName, $actionName, $select = 'id', $doer = NULL) { | |
56 | parent::__construct($entityName, $actionName, $select); | |
57 | $this->doer = $doer; | |
58 | } | |
59 | ||
60 | /** | |
61 | * We pass the doTask function an array representing one item to update. | |
62 | * We expect to get the same format back. | |
63 | * | |
64 | * @param \Civi\Api4\Generic\Result $result | |
65 | */ | |
66 | public function _run(Result $result) { | |
67 | foreach ($this->getBatchRecords() as $item) { | |
68 | $result[] = $this->doTask($item); | |
69 | } | |
70 | } | |
71 | ||
72 | /** | |
73 | * This Basic Batch class can be used in one of two ways: | |
74 | * | |
75 | * 1. Use this class directly by passing a callable ($doer) to the constructor. | |
76 | * 2. Extend this class and override this function. | |
77 | * | |
78 | * Either way, this function should return an array with an output record | |
79 | * for the item. | |
80 | * | |
81 | * @param array $item | |
82 | * @return array | |
83 | * @throws \Civi\API\Exception\NotImplementedException | |
84 | */ | |
85 | protected function doTask($item) { | |
86 | if (is_callable($this->doer)) { | |
87 | return call_user_func($this->doer, $item, $this); | |
88 | } | |
89 | throw new NotImplementedException('Doer function not found for api4 ' . $this->getEntityName() . '::' . $this->getActionName()); | |
90 | } | |
91 | ||
92 | } |