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

DatetimeUpdate::mutateAndGetPayload()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 37

Duplication

Lines 4
Ratio 10.81 %

Importance

Changes 0
Metric Value
cc 6
nc 1
nop 2
dl 4
loc 37
rs 8.7057
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 DatetimeUpdate {
13
14
	/**
15
	 * Defines the mutation data modification closure.
16
	 *
17
     * @param EEM_Datetime $model
18
     * @param Datetime $type
19
	 * @return callable
20
	 */
21
	public static function mutateAndGetPayload(EEM_Datetime $model, Datetime $type)
22
	{
23
		return static function ($input, AppContext $context, ResolveInfo $info) use ($model, $type)
24
		{
25
			/**
26
			 * Stop now if a user isn't allowed to create a datetime.
27
			 */
28 View Code Duplication
			if (! current_user_can('ee_edit_events')) {
29
				// translators: the %1$s is the name of the object being mutated
30
				throw new UserError(sprintf(__('Sorry, you are not allowed to edit %1$s', 'event_espresso' ), $type->name()));
31
			}
32
33
			$id = ! empty($input['id']) ? absint($input['id']) : 0;
34
			$entity = null;
35
36
			if ($id) {
37
				$entity = $model->get_one_by_ID($id);
38
			}
39
40
			/**
41
			 * If there's no existing datetime, throw an exception
42
			 */
43
			if (! $id || ! ($entity instanceof EE_Datetime)) {
44
				// translators: the placeholder is the name of the type being updated
45
				throw new UserError(sprintf(__( 'No %1$s could be found to update', 'event_espresso' ), $type->name()));
46
			}
47
48
			$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...
49
50
			// Update the entity
51
			$entity->save($args);
52
53
			return [
54
				'id' => $id,
55
			];
56
		};
57
	}
58
}
59