Completed
Branch EDTR/master (7b9c54)
by
unknown
28:28 queued 19:20
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
use DateTime;
7
use EEM_Price;
8
use EEM_Ticket;
9
use EE_Ticket;
10
use EventEspresso\core\domain\services\assets\EspressoEditorAssetManager;
11
use EventEspresso\core\domain\services\converters\RestApiSpoofer;
12
use EventEspresso\core\exceptions\InvalidDataTypeException;
13
use EventEspresso\core\exceptions\InvalidInterfaceException;
14
use EventEspresso\core\exceptions\ModelConfigurationException;
15
use EventEspresso\core\exceptions\RestPasswordIncorrectException;
16
use EventEspresso\core\exceptions\RestPasswordRequiredException;
17
use EventEspresso\core\exceptions\UnexpectedEntityException;
18
use EventEspresso\core\libraries\rest_api\RestException;
19
use InvalidArgumentException;
20
use ReflectionException;
21
22
/**
23
 * Class TicketMutation
24
 *
25
 * @package       Event Espresso
26
 * @author        Manzoor Wani
27
 */
28
class TicketMutation
29
{
30
31
    /**
32
     * Maps the GraphQL input to a format that the model functions can use
33
     *
34
     * @param array $input Data coming from the GraphQL mutation query input
35
     * @return array
36
     */
37
    public static function prepareFields(array $input)
38
    {
39
        $args = [];
40
41
        if (! empty($input['name'])) {
42
            $args['TKT_name'] = sanitize_text_field($input['name']);
43
        }
44
45
        if (! empty($input['description'])) {
46
            $args['TKT_description'] = sanitize_text_field($input['description']);
47
        }
48
49
        if (! empty($input['price'])) {
50
            $args['TKT_price'] = floatval($input['price']);
51
        }
52
53 View Code Duplication
        if (! empty($input['startDate'])) {
54
            $args['TKT_start_date'] = new DateTime(sanitize_text_field($input['startDate']));
55
        }
56
57 View Code Duplication
        if (! empty($input['endDate'])) {
58
            $args['TKT_end_date'] = new DateTime(sanitize_text_field($input['endDate']));
59
        }
60
61 View Code Duplication
        if (! empty($input['datetimes'])) {
62
            $args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']);
63
        }
64
65 View Code Duplication
        if (! empty($input['prices'])) {
66
            $args['prices'] = array_map('sanitize_text_field', (array) $input['prices']);
67
        }
68
69
        if (array_key_exists('isTrashed', $input)) {
70
            $args['TKT_deleted'] = (bool) $input['isTrashed'];
71
        }
72
73
        // Likewise the other fields...
74
75
        return $args;
76
    }
77
78
    /**
79
     * Sets the related datetimes for the given ticket.
80
     *
81
     * @param EE_Ticket $entity    The Ticket instance.
82
     * @param array     $datetimes Array of datetime IDs to relate.
83
     */
84 View Code Duplication
    public static function setRelatedDatetimes($entity, array $datetimes)
85
    {
86
        $relationName = 'Datetime';
87
        // Remove all the existing related datetimes
88
89
        $entity->_remove_relations($relationName);
90
        // @todo replace loop with single query
91
        foreach ($datetimes as $ID) {
92
            $parts = Relay::fromGlobalId($ID);
93
            if (! empty($parts['id']) && absint($parts['id'])) {
94
                $entity->_add_relation_to(
95
                    $parts['id'],
96
                    $relationName
97
                );
98
            }
99
        }
100
    }
101
102
    /**
103
     * Sets the related prices for the given ticket.
104
     *
105
     * @param EE_Ticket $entity The Ticket instance.
106
     * @param array     $prices Array of entity IDs to relate.
107
     */
108 View Code Duplication
    public static function setRelatedPrices($entity, array $prices)
109
    {
110
        $relationName = 'Price';
111
        // Remove all the existing related entities
112
        $entity->_remove_relations($relationName);
113
114
        // @todo replace loop with single query
115
        foreach ($prices as $ID) {
116
            $parts = Relay::fromGlobalId($ID);
117
            if (! empty($parts['id']) && absint($parts['id'])) {
118
                $entity->_add_relation_to(
119
                    $parts['id'],
120
                    $relationName
121
                );
122
            }
123
        }
124
    }
125
126
127
    /**
128
     * @param EE_Ticket $ticket_entity
129
     * @param EEM_Ticket $ticket_model
130
     * @throws DomainException
131
     * @throws EE_Error
132
     * @throws InvalidArgumentException
133
     * @throws InvalidDataTypeException
134
     * @throws InvalidInterfaceException
135
     * @throws ModelConfigurationException
136
     * @throws ReflectionException
137
     * @throws RestException
138
     * @throws UnexpectedEntityException
139
     * @since $VID:$
140
     */
141
    public static function addDefaultPrices(EE_Ticket $ticket_entity, EEM_Ticket $ticket_model)
142
    {
143
        $price_model = EEM_Price::instance();
144
        $default_prices = $price_model->get_all_default_prices();
145
        foreach ($default_prices as $default_price) {
0 ignored issues
show
Bug introduced by
The expression $default_prices of type integer|array<integer,object<EE_Base_Class>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
146
            $default_price->save();
147
            $default_price->_add_relation_to($ticket_entity, 'Ticket');
148
        }
149
    }
150
}
151