Completed
Branch EDTR/refactor-fast-api-fetch (278f65)
by
unknown
23:59 queued 15:37
created

TicketCreate::mutateAndGetPayload()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 41

Duplication

Lines 41
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 41
loc 41
rs 9.264
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\services\graphql\mutators;
3
4
use EEM_Ticket;
5
use EE_Ticket;
6
use EventEspresso\core\domain\services\graphql\types\Ticket;
7
use EventEspresso\core\domain\services\graphql\data\mutations\TicketMutation;
8
9
use EE_Error;
10
use InvalidArgumentException;
11
use ReflectionException;
12
use EventEspresso\core\exceptions\InvalidDataTypeException;
13
use EventEspresso\core\exceptions\InvalidInterfaceException;
14
15
use GraphQL\Type\Definition\ResolveInfo;
16
use WPGraphQL\AppContext;
17
use GraphQL\Error\UserError;
18
19 View Code Duplication
class TicketCreate
20
{
21
22
	/**
23
	 * Defines the mutation data modification closure.
24
	 *
25
     * @param EEM_Ticket $model
26
     * @param Ticket $type
27
	 * @return callable
28
	 */
29
	public static function mutateAndGetPayload(EEM_Ticket $model, Ticket $type)
30
	{
31
		/**
32
		 * Creates an entity.
33
		 *
34
		 * @param array         $input   The input for the mutation
35
		 * @param AppContext    $context The AppContext passed down to all resolvers
36
		 * @param ResolveInfo   $info    The ResolveInfo passed down to all resolvers
37
		 *
38
		 * @throws UserError
39
		 * @throws ReflectionException
40
		 * @throws InvalidArgumentException
41
		 * @throws InvalidInterfaceException
42
		 * @throws InvalidDataTypeException
43
		 * @throws EE_Error
44
		 */
45
		return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type)
46
		{
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(sprintf(__('Sorry, you are not allowed to create %1$s', 'event_espresso' ), $type->name()));
54
			}
55
56
			$args = TicketMutation::prepare_fields($input);
57
58
			$entity = EE_Ticket::new_instance($args);
59
			$id = $entity->save();
60
61
			if (empty($id)) {
62
				throw new UserError(__( 'The object failed to create but no error was provided', 'event_espresso'));
63
			}
64
65
			return [
66
				'id' => $id,
67
			];
68
		};
69
	}
70
}
71