1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\mutators; |
4
|
|
|
|
5
|
|
|
use EE_Base_Class; |
6
|
|
|
use EEM_Base; |
7
|
|
|
use Exception; |
8
|
|
|
use GraphQL\Error\UserError; |
9
|
|
|
use GraphQLRelay\Relay; |
10
|
|
|
use OutOfBoundsException; |
11
|
|
|
use RuntimeException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class EntityMutator |
15
|
|
|
* Description |
16
|
|
|
* |
17
|
|
|
* @package EventEspresso\core\domain\services\graphql\mutators |
18
|
|
|
* @author Brent Christensen |
19
|
|
|
* @since $VID:$ |
20
|
|
|
*/ |
21
|
|
|
abstract class EntityMutator |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param EEM_Base $model |
26
|
|
|
* @param integer $ID |
27
|
|
|
* @param string $capability |
28
|
|
|
* @return EE_Base_Class |
29
|
|
|
* @throws OutOfBoundsException |
30
|
|
|
* @throws UserError |
31
|
|
|
* @since $VID:$ |
32
|
|
|
*/ |
33
|
|
|
protected static function getEntityFromID(EEM_Base $model, $ID, $capability = 'ee_edit_events') |
34
|
|
|
{ |
35
|
|
|
EntityMutator::checkPermissions($model, $capability); |
36
|
|
|
return EntityMutator::getEntity($model, $ID); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param EEM_Base $model |
41
|
|
|
* @param array $input |
42
|
|
|
* @param string $capability |
43
|
|
|
* @return EE_Base_Class |
44
|
|
|
* @throws OutOfBoundsException |
45
|
|
|
* @throws UserError |
46
|
|
|
* @since $VID:$ |
47
|
|
|
*/ |
48
|
|
|
protected static function getEntityFromInputData(EEM_Base $model, array $input, $capability = 'ee_edit_events') |
49
|
|
|
{ |
50
|
|
|
EntityMutator::checkPermissions($model, $capability); |
51
|
|
|
$ID = EntityMutator::getEntityIDFromGlobalId($model, $input); |
52
|
|
|
return EntityMutator::getEntity($model, $ID); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param EEM_Base $model |
58
|
|
|
* @param string $capability |
59
|
|
|
* @throws UserError |
60
|
|
|
* @since $VID:$ |
61
|
|
|
*/ |
62
|
|
|
protected static function checkPermissions(EEM_Base $model, $capability = 'ee_edit_events') |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* Stop now if a user isn't allowed to execute mutation |
66
|
|
|
*/ |
67
|
|
|
if (! current_user_can($capability)) { |
68
|
|
|
$model_name = $model->get_this_model_name(); |
69
|
|
|
$message = sprintf( |
70
|
|
|
esc_html_x( |
71
|
|
|
'We\'re sorry but you do not have the required permissions to execute %1$s mutations!', |
72
|
|
|
'We\'re sorry but you do not have the required permissions to execute entity(datetime/ticket/etc) mutations!', |
73
|
|
|
'event_espresso' |
74
|
|
|
), |
75
|
|
|
strtolower($model_name) |
76
|
|
|
); |
77
|
|
|
throw new UserError($message); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param EEM_Base $model |
83
|
|
|
* @param array $input |
84
|
|
|
* @return int |
85
|
|
|
* @throws OutOfBoundsException |
86
|
|
|
* @since $VID:$ |
87
|
|
|
*/ |
88
|
|
|
protected static function getEntityIDFromGlobalId(EEM_Base $model, array $input) |
89
|
|
|
{ |
90
|
|
|
$id_parts = ! empty($input['id']) ? Relay::fromGlobalId($input['id']) : null; |
91
|
|
|
$id = ! empty($id_parts['id']) ? absint($id_parts['id']) : 0; |
92
|
|
|
if ($id > 0) { |
93
|
|
|
return $id; |
94
|
|
|
} |
95
|
|
|
// no ID? throw an exception |
96
|
|
|
$model_name = $model->get_this_model_name(); |
97
|
|
|
throw new OutOfBoundsException( |
98
|
|
|
sprintf( |
99
|
|
|
esc_html_x( |
100
|
|
|
'A missing or invalid %1$s ID was received.', |
101
|
|
|
'A missing or invalid entity(datetime/ticket/etc) ID was received.', |
102
|
|
|
'event_espresso' |
103
|
|
|
), |
104
|
|
|
strtolower($model_name) |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param EEM_Base $model |
112
|
|
|
* @param int $ID |
113
|
|
|
* @return EE_Base_Class |
114
|
|
|
* @throws OutOfBoundsException |
115
|
|
|
* @since $VID:$ |
116
|
|
|
*/ |
117
|
|
|
protected static function getEntity(EEM_Base $model, $ID = 0) |
118
|
|
|
{ |
119
|
|
|
$entity = $model->get_one_by_ID($ID); |
120
|
|
|
$model_name = $model->get_this_model_name(); |
121
|
|
|
$class_name = 'EE_' . $model_name; |
122
|
|
|
if ($entity instanceof $class_name) { |
123
|
|
|
return $entity; |
124
|
|
|
} |
125
|
|
|
// OOPS!!! invalid entity |
126
|
|
|
throw new OutOfBoundsException( |
127
|
|
|
sprintf( |
128
|
|
|
esc_html_x( |
129
|
|
|
'A valid %1$s could not be retrieved from the database.', |
130
|
|
|
'A valid entity(datetime/ticket/etc) could not be found in the database.', |
131
|
|
|
'event_espresso' |
132
|
|
|
), |
133
|
|
|
strtolower($model_name) |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $results |
141
|
|
|
* @param string $message |
142
|
|
|
* @throws RuntimeException |
143
|
|
|
* @since $VID:$ |
144
|
|
|
*/ |
145
|
|
|
protected static function validateResults($results, $message = '') |
146
|
|
|
{ |
147
|
|
|
if (empty($results)) { |
148
|
|
|
$message = $message !== '' |
149
|
|
|
? $message |
150
|
|
|
: esc_html__( |
151
|
|
|
'An unknown error occurred. Please check your server\'s error logs for more information', |
152
|
|
|
'event_espresso' |
153
|
|
|
); |
154
|
|
|
throw new RuntimeException($message); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param Exception $exception |
161
|
|
|
* @param string $message_prefix |
162
|
|
|
* @throws RuntimeException |
163
|
|
|
* @since $VID:$ |
164
|
|
|
*/ |
165
|
|
|
protected static function handleExceptions(Exception $exception, $message_prefix = '') |
166
|
|
|
{ |
167
|
|
|
$message_prefix = $message_prefix !== '' |
168
|
|
|
? $message_prefix |
169
|
|
|
: esc_html__( |
170
|
|
|
'The mutation could not be executed because of the following error(s)', |
171
|
|
|
'event_espresso' |
172
|
|
|
); |
173
|
|
|
throw new RuntimeException( |
174
|
|
|
sprintf('%1$s: %2$s', $message_prefix, $exception->getMessage()) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|