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

EventUpdate   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_Event;
5
use EE_Event;
6
use WP_Post_Type;
7
use EventEspresso\core\domain\services\graphql\types\Event;
8
use EventEspresso\core\domain\services\graphql\data\mutations\EventMutation;
9
use GraphQL\Type\Definition\ResolveInfo;
10
use WPGraphQL\AppContext;
11
use GraphQL\Error\UserError;
12
13 View Code Duplication
class EventUpdate {
14
15
	/**
16
	 * Defines the mutation data modification closure.
17
	 *
18
     * @param EEM_Event $model
19
     * @param Event $type
20
	 * @return callable
21
	 */
22
	public static function mutateFields(EEM_Event $model, Event $type)
23
	{
24
		/**
25
		 * Update additional data related to the entity.
26
		 *
27
		 * @param int           $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
			$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);
49
50
			if ($entity instanceof EE_Event) {
51
				$args = EventMutation::prepare_fields($input, $mutation_name);
52
	
53
				// Update the entity
54
				$entity->save($args);
55
			}
56
		};
57
	}
58
}
59