1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\mutators; |
4
|
|
|
|
5
|
|
|
use EEM_Ticket; |
6
|
|
|
use EE_Ticket; |
7
|
|
|
use EventEspresso\core\domain\services\graphql\types\Ticket; |
8
|
|
|
use EventEspresso\core\domain\services\graphql\data\mutations\TicketMutation; |
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
|
|
|
|
20
|
|
View Code Duplication |
class TicketCreate |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Defines the mutation data modification closure. |
25
|
|
|
* |
26
|
|
|
* @param EEM_Ticket $model |
27
|
|
|
* @param Ticket $type |
28
|
|
|
* @return callable |
29
|
|
|
*/ |
30
|
|
|
public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type) |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Creates an entity. |
34
|
|
|
* |
35
|
|
|
* @param array $input The input for the mutation |
36
|
|
|
* @param AppContext $context The AppContext passed down to all resolvers |
37
|
|
|
* @param ResolveInfo $info The ResolveInfo passed down to all resolvers |
38
|
|
|
* @return array |
39
|
|
|
* @throws UserError |
40
|
|
|
* @throws ReflectionException |
41
|
|
|
* @throws InvalidArgumentException |
42
|
|
|
* @throws InvalidInterfaceException |
43
|
|
|
* @throws InvalidDataTypeException |
44
|
|
|
* @throws EE_Error |
45
|
|
|
*/ |
46
|
|
|
return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type) { |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Stop now if a user isn't allowed to create a datetime. |
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(__('Sorry, you are not allowed to create %1$s', 'event_espresso'), $type->name()) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$datetimes = []; |
59
|
|
|
|
60
|
|
|
$args = TicketMutation::prepareFields($input); |
61
|
|
|
|
62
|
|
|
if (isset($args['datetimes'])) { |
63
|
|
|
$datetimes = $args['datetimes']; |
64
|
|
|
unset($args['datetimes']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$entity = EE_Ticket::new_instance($args); |
68
|
|
|
$id = $entity->save(); |
69
|
|
|
|
70
|
|
|
if (empty($id)) { |
71
|
|
|
throw new UserError(__('The object failed to create but no error was provided', 'event_espresso')); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! empty($datetimes)) { |
75
|
|
|
TicketMutation::setRelatedDatetimes($entity, $datetimes); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return [ |
79
|
|
|
'id' => $id, |
80
|
|
|
]; |
81
|
|
|
}; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|