1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\mutators; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
8
|
|
|
use WPGraphQL\AppContext; |
9
|
|
|
|
10
|
|
|
class BulkEntityMutator extends EntityMutator |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var callable $entity_mutator . |
15
|
|
|
*/ |
16
|
|
|
protected $entity_mutator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* BulkEntityMutator constructor. |
20
|
|
|
* |
21
|
|
|
* @param array $entity_mutator The mutator for an entity. |
22
|
|
|
*/ |
23
|
|
|
public function __construct(callable $entity_mutator) |
24
|
|
|
{ |
25
|
|
|
$this->entity_mutator = $entity_mutator; |
26
|
|
|
} |
27
|
|
|
/** |
28
|
|
|
* Bulk updates entities. |
29
|
|
|
* |
30
|
|
|
* @param array $input The input for the mutation |
31
|
|
|
* @param AppContext $context The AppContext passed down to all resolvers |
32
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function updateEntities($input, AppContext $context, ResolveInfo $info) |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
$updated = []; |
39
|
|
|
$failed = []; |
40
|
|
|
// TODO Add meaningful error messages for every failure. |
41
|
|
|
// $errors = []; |
42
|
|
|
|
43
|
|
|
try { |
44
|
|
|
if (empty($input['uniqueInputs']) || !is_array($input['uniqueInputs'])) { |
45
|
|
|
throw new OutOfBoundsException( |
46
|
|
|
esc_html__('A valid input was not provided.', 'event_espresso') |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$sharedInput = ! empty($input['sharedInput']) ? $input['sharedInput'] : []; |
51
|
|
|
|
52
|
|
|
foreach ($input['uniqueInputs'] as $uniqueInput) { |
53
|
|
|
try { |
54
|
|
|
// values in $uniqueInput will override those in $sharedInput |
55
|
|
|
$finalInput = array_merge($sharedInput, $uniqueInput); |
56
|
|
|
// mutate the individual entity. |
57
|
|
|
$mutator = $this->entity_mutator; |
58
|
|
|
$mutator($finalInput, $context, $info); |
59
|
|
|
// we are here it means the update was successful. |
60
|
|
|
$updated[] = $uniqueInput['id']; |
61
|
|
|
} catch (Exception $e) { |
62
|
|
|
// sorry mate, couldn't help you :( |
63
|
|
|
$failed[] = $uniqueInput['id']; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} catch (Exception $exception) { |
67
|
|
|
EntityMutator::handleExceptions( |
68
|
|
|
$exception, |
69
|
|
|
esc_html__( |
70
|
|
|
'Could not perform the update because of the following error(s)', |
71
|
|
|
'event_espresso' |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return compact('updated', 'failed'); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|