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

DatetimeCreate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 10.53 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A mutateAndGetPayload() 4 27 3

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 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