|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\mutators; |
|
4
|
|
|
|
|
5
|
|
|
use EEM_Price; |
|
6
|
|
|
use EE_Price; |
|
7
|
|
|
use EventEspresso\core\domain\services\graphql\types\Price; |
|
8
|
|
|
use EventEspresso\core\domain\services\graphql\data\mutations\PriceMutation; |
|
9
|
|
|
|
|
10
|
|
|
use EE_Error; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use ReflectionException; |
|
13
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
|
14
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
|
15
|
|
|
|
|
16
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
17
|
|
|
use WPGraphQL\AppContext; |
|
18
|
|
|
use GraphQL\Error\UserError; |
|
19
|
|
|
use GraphQLRelay\Relay; |
|
20
|
|
|
|
|
21
|
|
|
class PriceUpdate |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Defines the mutation data modification closure. |
|
26
|
|
|
* |
|
27
|
|
|
* @param EEM_Price $model |
|
28
|
|
|
* @param Price $type |
|
29
|
|
|
* @return callable |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function mutateAndGetPayload(EEM_Price $model, Price $type) |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Updates an entity. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $input The input for the mutation |
|
37
|
|
|
* @param AppContext $context The AppContext passed down to all resolvers |
|
38
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
|
39
|
|
|
* @return array |
|
40
|
|
|
* @throws UserError |
|
41
|
|
|
* @throws ReflectionException |
|
42
|
|
|
* @throws InvalidArgumentException |
|
43
|
|
|
* @throws InvalidInterfaceException |
|
44
|
|
|
* @throws InvalidDataTypeException |
|
45
|
|
|
* @throws EE_Error |
|
46
|
|
|
*/ |
|
47
|
|
|
return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) { |
|
48
|
|
|
/** |
|
49
|
|
|
* Stop now if a user isn't allowed to create an entity. |
|
50
|
|
|
*/ |
|
51
|
|
|
if (! current_user_can('ee_edit_events')) { |
|
52
|
|
|
// translators: the %1$s is the name of the object being mutated |
|
53
|
|
|
throw new UserError( |
|
54
|
|
|
sprintf(esc_html__('Sorry, you are not allowed to edit %1$s', 'event_espresso'), $type->name()) |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
$id_parts = ! empty($input['id']) ? Relay::fromGlobalId($input['id']) : null; |
|
58
|
|
|
|
|
59
|
|
|
$id = ! empty($id_parts['id']) ? absint($id_parts['id']) : 0; |
|
60
|
|
|
$entity = null; |
|
61
|
|
|
|
|
62
|
|
|
if ($id) { |
|
63
|
|
|
$entity = $model->get_one_by_ID($id); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* If there's no existing entity, throw an exception |
|
68
|
|
|
*/ |
|
69
|
|
|
if (! $id || ! ($entity instanceof EE_Price)) { |
|
70
|
|
|
// translators: the placeholder is the name of the type being updated |
|
71
|
|
|
throw new UserError( |
|
72
|
|
|
sprintf(esc_html__('No %1$s could be found to update', 'event_espresso'), $type->name()) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$args = PriceMutation::prepareFields($input); |
|
77
|
|
|
|
|
78
|
|
|
// Update the entity |
|
79
|
|
|
$result = $entity->save($args); |
|
80
|
|
|
|
|
81
|
|
|
if (empty($result)) { |
|
82
|
|
|
throw new UserError(esc_html__('The object failed to update but no error was provided', 'event_espresso')); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return [ |
|
86
|
|
|
'id' => $id, |
|
87
|
|
|
]; |
|
88
|
|
|
}; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|