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

DatetimeUpdate   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 8.51 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 4
loc 47
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B mutateAndGetPayload() 4 37 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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