Completed
Branch EDTR/master (7b9c54)
by
unknown
28:28 queued 19:20
created

DatetimeMutation   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 37.18 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 29
loc 78
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
D prepareFields() 13 39 11
A setRelatedTickets() 16 16 4

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
use DateTime;
6
use EE_Datetime;
7
use EE_Error;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use GraphQLRelay\Relay;
11
use InvalidArgumentException;
12
use ReflectionException;
13
14
/**
15
 * Class DatetimeMutation
16
 *
17
 * @package       Event Espresso
18
 * @author        Manzoor Wani
19
 */
20
class DatetimeMutation
21
{
22
23
    /**
24
     * Maps the GraphQL input to a format that the model functions can use
25
     *
26
     * @param array $input Data coming from the GraphQL mutation query input
27
     * @return array
28
     */
29
    public static function prepareFields(array $input)
30
    {
31
        $args = [];
32
33
        if (! empty($input['eventId'])) {
34
            $args['EVT_ID'] = absint($input['eventId']);
35 View Code Duplication
        } elseif (! empty($input['event'])) {
36
            $parts = Relay::fromGlobalId($input['event']);
37
            $args['EVT_ID'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null;
38
        }
39
40
        if (! empty($input['name'])) {
41
            $args['DTT_name'] = sanitize_text_field($input['name']);
42
        }
43
44
        if (! empty($input['description'])) {
45
            $args['DTT_description'] = sanitize_text_field($input['description']);
46
        }
47
48 View Code Duplication
        if (! empty($input['startDate'])) {
49
            $args['DTT_EVT_start'] = new DateTime(sanitize_text_field($input['startDate']));
50
        }
51
52 View Code Duplication
        if (! empty($input['endDate'])) {
53
            $args['DTT_EVT_end'] = new DateTime(sanitize_text_field($input['endDate']));
54
        }
55
56 View Code Duplication
        if (! empty($input['tickets'])) {
57
            $args['tickets'] = array_map('sanitize_text_field', (array) $input['tickets']);
58
        }
59
60
        if (array_key_exists('isTrashed', $input)) {
61
            $args['DTT_deleted'] = (bool) $input['isTrashed'];
62
        }
63
64
        // Likewise the other fields...
65
66
        return $args;
67
    }
68
69
70
    /**
71
     * Sets the related tickets for the given datetime.
72
     *
73
     * @param EE_Datetime $entity  The datetime instance.
74
     * @param array       $tickets Array of ticket IDs to relate.
75
     * @throws EE_Error
76
     * @throws InvalidDataTypeException
77
     * @throws InvalidInterfaceException
78
     * @throws InvalidArgumentException
79
     * @throws ReflectionException
80
     */
81 View Code Duplication
    public static function setRelatedTickets($entity, array $tickets)
82
    {
83
        $relationName = 'Ticket';
84
        // Remove all the existing related tickets
85
        $entity->_remove_relations($relationName);
86
87
        foreach ($tickets as $ID) {
88
            $parts = Relay::fromGlobalId($ID);
89
            if (! empty($parts['id']) && absint($parts['id'])) {
90
                $entity->_add_relation_to(
91
                    $parts['id'],
92
                    $relationName
93
                );
94
            }
95
        }
96
    }
97
}
98