Completed
Branch EDTR/master (bb716e)
by
unknown
57:55 queued 49:04
created

DatetimeMutation::prepareFields()   C

Complexity

Conditions 10
Paths 192

Size

Total Lines 36

Duplication

Lines 3
Ratio 8.33 %

Importance

Changes 0
Metric Value
cc 10
nc 192
nop 1
dl 3
loc 36
rs 6.9
c 0
b 0
f 0

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