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

TicketMutation::setRelatedDatetimes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\graphql\data\mutations;
4
5
/**
6
 * Class TicketMutation
7
 *
8
 * @package       Event Espresso
9
 * @author        Manzoor Wani
10
 */
11
class TicketMutation
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['name'])) {
26
            $args['TKT_name'] = sanitize_text_field($input['name']);
27
        }
28
29
        if (! empty($input['description'])) {
30
            $args['TKT_description'] = sanitize_text_field($input['description']);
31
        }
32
33
        if (! empty($input['price'])) {
34
            $args['TKT_price'] = floatval($input['price']);
35
        }
36
37 View Code Duplication
        if (! empty($input['datetimes'])) {
38
            $args['datetimes'] = array_filter(array_map('absint', (array) $input['datetimes']));
39
        }
40
41
        // Likewise the other fields...
42
43
        return $args;
44
    }
45
46
    /**
47
     * Sets the related tickets for the given datetime.
48
     *
49
     * @param EE_Ticket $entity    The Ticket instance.
50
     * @param array     $datetimes Array of datetime IDs to relate.
51
     */
52
    public static function setRelatedDatetimes($entity, array $datetimes)
53
    {
54
        $relationName = 'Datetime';
55
        // Remove all the existing related datetimes
56
        $entity->_remove_relations($relationName);
57
58
        foreach ($datetimes as $ID) {
59
            $entity->_add_relation_to(
60
                $ID,
61
                $relationName
62
            );
63
        }
64
    }
65
}
66