Completed
Branch EDTR/master (872e60)
by
unknown
27:04 queued 17:58
created

TicketMutation::setRelatedPrices()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 2
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\data\mutations;
4
5
use GraphQLRelay\Relay;
6
7
/**
8
 * Class TicketMutation
9
 *
10
 * @package       Event Espresso
11
 * @author        Manzoor Wani
12
 */
13
class TicketMutation
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['name'])) {
28
            $args['TKT_name'] = sanitize_text_field($input['name']);
29
        }
30
31
        if (! empty($input['description'])) {
32
            $args['TKT_description'] = sanitize_text_field($input['description']);
33
        }
34
35
        if (! empty($input['price'])) {
36
            $args['TKT_price'] = floatval($input['price']);
37
        }
38
39 View Code Duplication
        if (! empty($input['datetimes'])) {
40
            $args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']);
41
        }
42
43 View Code Duplication
        if (! empty($input['prices'])) {
44
            $args['prices'] = array_map('sanitize_text_field', (array) $input['prices']);
45
        }
46
47
        // Likewise the other fields...
48
49
        return $args;
50
    }
51
52
    /**
53
     * Sets the related datetimes for the given ticket.
54
     *
55
     * @param EE_Ticket $entity    The Ticket instance.
56
     * @param array     $datetimes Array of datetime IDs to relate.
57
     */
58 View Code Duplication
    public static function setRelatedDatetimes($entity, array $datetimes)
59
    {
60
        $relationName = 'Datetime';
61
        // Remove all the existing related datetimes
62
63
        $entity->_remove_relations($relationName);
64
        // @todo replace loop with single query
65
        foreach ($datetimes as $ID) {
66
            $parts = Relay::fromGlobalId($ID);
67
            if (! empty($parts['id']) && absint($parts['id'])) {
68
                $entity->_add_relation_to(
69
                    $parts['id'],
70
                    $relationName
71
                );
72
            }
73
        }
74
    }
75
76
    /**
77
     * Sets the related prices for the given ticket.
78
     *
79
     * @param EE_Ticket $entity The Ticket instance.
80
     * @param array     $prices Array of entity IDs to relate.
81
     */
82 View Code Duplication
    public static function setRelatedPrices($entity, array $prices)
83
    {
84
        $relationName = 'Price';
85
        // Remove all the existing related entities
86
        $entity->_remove_relations($relationName);
87
88
        // @todo replace loop with single query
89
        foreach ($prices as $ID) {
90
            $parts = Relay::fromGlobalId($ID);
91
            if (! empty($parts['id']) && absint($parts['id'])) {
92
                $entity->_add_relation_to(
93
                    $parts['id'],
94
                    $relationName
95
                );
96
            }
97
        }
98
    }
99
}
100