Completed
Branch EDTR/gql-server-side (10cf63)
by
unknown
27:17 queued 18:38
created

DatetimeMutation   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 4.76 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 3
loc 63
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareFields() 3 33 7
A setRelatedTickets() 0 13 2

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
3
namespace EventEspresso\core\domain\services\graphql\data\mutations;
4
5
/**
6
 * Class DatetimeMutation
7
 *
8
 * @package       Event Espresso
9
 * @author        Manzoor Wani
10
 */
11
class DatetimeMutation
12
{
13
14
    /**
15
     * Maps the GraphQL input to a format that the model functions can use
16
     *
17
     * @param array $input Data coming from the GraphQL mutation query input
18
     * @return array
19
     */
20
    public static function prepareFields(array $input)
21
    {
22
23
        $args = [];
24
25
        if (! empty($input['event'])) {
26
            $args['EVT_ID'] = absint($input['event']);
27
        }
28
29
        if (! empty($input['name'])) {
30
            $args['DTT_name'] = sanitize_text_field($input['name']);
31
        }
32
33
        if (! empty($input['description'])) {
34
            $args['DTT_description'] = sanitize_text_field($input['description']);
35
        }
36
37
        if (! empty($input['startDate'])) {
38
            $args['DTT_EVT_start'] = sanitize_text_field($input['startDate']);
39
        }
40
41
        if (! empty($input['endDate'])) {
42
            $args['DTT_EVT_end'] = sanitize_text_field($input['endDate']);
43
        }
44
45 View Code Duplication
        if (! empty($input['tickets'])) {
46
            $args['tickets'] = array_filter(array_map('absint', (array) $input['tickets']));
47
        }
48
49
        // Likewise the other fields...
50
51
        return $args;
52
    }
53
54
    /**
55
     * Sets the related tickets for the given datetime.
56
     *
57
     * @param EE_Datetime $entity  The datetime instance.
58
     * @param array       $tickets Array of ticket IDs to relate.
59
     */
60
    public static function setRelatedTickets($entity, array $tickets)
61
    {
62
        $relationName = 'Ticket';
63
        // Remove all the existing related tickets
64
        $entity->_remove_relations($relationName);
65
66
        foreach ($tickets as $ID) {
67
            $entity->_add_relation_to(
68
                $ID,
69
                $relationName
70
            );
71
        }
72
    }
73
}
74