Completed
Branch EDTR/master (08a293)
by
unknown
09:29 queued 40s
created

DatetimeMutation::prepareFields()   F

Complexity

Conditions 19
Paths > 20000

Size

Total Lines 62

Duplication

Lines 17
Ratio 27.42 %

Importance

Changes 0
Metric Value
cc 19
nc 61440
nop 1
dl 17
loc 62
rs 0.3499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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