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

VenueUpdate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A mutateFields() 36 36 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_Venue;
5
use EE_Venue;
6
use WP_Post_Type;
7
use EventEspresso\core\domain\services\graphql\types\Venue;
8
use EventEspresso\core\domain\services\graphql\data\mutations\VenueMutation;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use WPGraphQL\AppContext;
11
use GraphQL\Error\UserError;
12
13 View Code Duplication
class VenueUpdate {
14
15
	/**
16
	 * Defines the mutation data modification closure.
17
	 *
18
     * @param EEM_Venue $model
19
     * @param Venue $type
20
	 * @return callable
21
	 */
22
	public static function mutateFields(EEM_Venue $model, Venue $type)
23
	{
24
		/**
25
		 * Update additional data related to the entity.
26
		 *
27
		 * @param int           $post_id              The ID of the postObject being mutated
28
		 * @param array         $input                The input for the mutation
29
		 * @param WP_Post_Type  $post_type_object     The Post Type Object for the type of post being mutated
30
		 * @param string        $mutation_name        The name of the mutation (ex: create, update, delete)
31
		 * @param AppContext    $context              The AppContext passed down to all resolvers
32
		 * @param ResolveInfo   $info                 The ResolveInfo passed down to all resolvers
33
		 */
34
		return static function (
35
			$post_id,
36
			array $input,
37
			WP_Post_Type $post_type_object,
38
			$mutation_name,
39
			AppContext $context,
40
			ResolveInfo $info
41
		) use ($model, $type)
42
		{
43
			// Make sure we are dealing with the right entity.
44
			if ($post_type_object->graphql_single_name !== $type->name()) {
45
				return;
46
			}
47
48
			$entity = $model->get_one_by_ID($id);
0 ignored issues
show
Bug introduced by
The variable $id 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
			if ($entity instanceof EE_Venue) {
51
				$args = VenueMutation::prepare_fields($input, $mutation_name);
52
	
53
				// Update the entity
54
				$entity->save($args);
55
			}
56
		};
57
	}
58
}
59