Completed
Branch EDTR/refactor-fast-api-fetch (d0e0df)
by
unknown
09:08 queued 34s
created

DatetimeCreate::mutateAndGetPayload()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 27

Duplication

Lines 4
Ratio 14.81 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 2
dl 4
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\services\graphql\mutators;
3
4
use EEM_Datetime;
5
use EE_Datetime;
6
use EventEspresso\core\domain\services\graphql\types\Datetime;
7
use EventEspresso\core\domain\services\graphql\data\mutations\DatetimeMutation;
8
use GraphQL\Type\Definition\ResolveInfo;
9
use WPGraphQL\AppContext;
10
use GraphQL\Error\UserError;
11
12
class DatetimeCreate
13
{
14
15
	/**
16
	 * Defines the mutation data modification closure.
17
	 *
18
     * @param EEM_Datetime $model
19
     * @param Datetime $type
20
	 * @return callable
21
	 */
22
	public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type)
23
	{
24
		return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type)
25
		{
26
27
			/**
28
			 * Stop now if a user isn't allowed to create a datetime.
29
			 */
30 View Code Duplication
			if (! current_user_can('ee_edit_events')) {
31
				// translators: the %1$s is the name of the object being mutated
32
				throw new UserError(sprintf(__('Sorry, you are not allowed to create %1$s', 'event_espresso' ), $type->name()));
33
			}
34
35
			$args = DatetimeMutation::prepare_fields($input, $mutation_name);
0 ignored issues
show
Bug introduced by
The variable $mutation_name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
36
37
			$entity = EE_Datetime::new_instance($args);
38
			$id = $entity->save();
39
40
			if (empty($id)) {
41
				throw new UserError(__( 'The object failed to create but no error was provided', 'event_espresso'));
42
			}
43
44
			return [
45
				'id' => $id,
46
			];
47
		};
48
	}
49
}
50