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

TicketMutation::prepareFields()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 96

Duplication

Lines 20
Ratio 20.83 %

Importance

Changes 0
Metric Value
cc 27
nc 26214400
nop 1
dl 20
loc 96
rs 0
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 DomainException;
6
use EE_Error;
7
use Exception;
8
use GraphQLRelay\Relay;
9
use DateTime;
10
use EEM_Price;
11
use EEM_Ticket;
12
use EE_Ticket;
13
use EventEspresso\core\exceptions\InvalidDataTypeException;
14
use EventEspresso\core\exceptions\InvalidInterfaceException;
15
use EventEspresso\core\exceptions\ModelConfigurationException;
16
use EventEspresso\core\exceptions\UnexpectedEntityException;
17
use EventEspresso\core\libraries\rest_api\RestException;
18
use InvalidArgumentException;
19
use ReflectionException;
20
21
/**
22
 * Class TicketMutation
23
 *
24
 * @package       Event Espresso
25
 * @author        Manzoor Wani
26
 */
27
class TicketMutation
28
{
29
30
    /**
31
     * Maps the GraphQL input to a format that the model functions can use
32
     *
33
     * @param array $input Data coming from the GraphQL mutation query input
34
     * @return array
35
     * @throws Exception
36
     */
37
    public static function prepareFields(array $input)
38
    {
39
        $args = [];
40
41 View Code Duplication
        if (! empty($input['datetimes'])) {
42
            $args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']);
43
        }
44
45
        if (! empty($input['description'])) {
46
            $args['TKT_description'] = sanitize_text_field($input['description']);
47
        }
48
49 View Code Duplication
        if (! empty($input['endDate'])) {
50
            $args['TKT_end_date'] = new DateTime(sanitize_text_field($input['endDate']));
51
        }
52
53
        if (array_key_exists('isDefault', $input)) {
54
            $args['TKT_is_default'] = (bool) $input['isDefault'];
55
        }
56
57
        if (array_key_exists('isRequired', $input)) {
58
            $args['TKT_required'] = (bool) $input['isRequired'];
59
        }
60
61
        if (array_key_exists('isTaxable', $input)) {
62
            $args['TKT_taxable'] = (bool) $input['isTaxable'];
63
        }
64
65
        if (array_key_exists('isTrashed', $input)) {
66
            $args['TKT_deleted'] = (bool) $input['isTrashed'];
67
        }
68
69
        if (array_key_exists('max', $input)) {
70
            $args['TKT_max'] = (int) $input['max'];
71
        }
72
73
        if (array_key_exists('min', $input)) {
74
            $args['TKT_min'] = (int) $input['min'];
75
        }
76
77
        if (! empty($input['name'])) {
78
            $args['TKT_name'] = sanitize_text_field($input['name']);
79
        }
80
81
        if (array_key_exists('order', $input)) {
82
            $args['TKT_order'] = (int) $input['order'];
83
        }
84
85 View Code Duplication
        if (! empty($input['parent'])) {
86
            $parts = Relay::fromGlobalId(sanitize_text_field($input['parent']));
87
            $args['TKT_parent'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null;
88
        }
89
90
        if (! empty($input['price'])) {
91
            $args['TKT_price'] = (float) $input['price'];
92
        }
93
94 View Code Duplication
        if (! empty($input['prices'])) {
95
            $args['prices'] = array_map('sanitize_text_field', (array) $input['prices']);
96
        }
97
98
        if (array_key_exists('quantity', $input)) {
99
            $args['TKT_qty'] = (int) $input['quantity'];
100
        }
101
102
        if (array_key_exists('reserved', $input)) {
103
            $args['TKT_reserved'] = (int) $input['reserved'];
104
        }
105
106
        if (array_key_exists('reverseCalculate', $input)) {
107
            $args['TKT_reverse_calculate'] = (bool) $input['reverseCalculate'];
108
        }
109
110
        if (array_key_exists('row', $input)) {
111
            $args['TKT_row'] = (int) $input['row'];
112
        }
113
114
        if (array_key_exists('sold', $input)) {
115
            $args['TKT_sold'] = (int) $input['sold'];
116
        }
117
118 View Code Duplication
        if (! empty($input['startDate'])) {
119
            $args['TKT_start_date'] = new DateTime(sanitize_text_field($input['startDate']));
120
        }
121
122
        if (array_key_exists('uses', $input)) {
123
            $args['TKT_uses'] = (int) $input['uses'];
124
        }
125
126 View Code Duplication
        if (! empty($input['wpUser'])) {
127
            $parts = Relay::fromGlobalId(sanitize_text_field($input['wpUser']));
128
            $args['TKT_wp_user'] = (! empty($parts['id']) && is_int($parts['id'])) ? $parts['id'] : null;
129
        }
130
131
        return $args;
132
    }
133
134
135
    /**
136
     * Sets the related datetimes for the given ticket.
137
     *
138
     * @param EE_Ticket $entity    The Ticket instance.
139
     * @param array     $datetimes Array of datetime IDs to relate.
140
     * @throws EE_Error
141
     * @throws InvalidArgumentException
142
     * @throws InvalidDataTypeException
143
     * @throws InvalidInterfaceException
144
     * @throws ReflectionException
145
     */
146 View Code Duplication
    public static function setRelatedDatetimes($entity, array $datetimes)
147
    {
148
        $relationName = 'Datetime';
149
        // Remove all the existing related datetimes
150
151
        $entity->_remove_relations($relationName);
152
        // @todo replace loop with single query
153
        foreach ($datetimes as $ID) {
154
            $parts = Relay::fromGlobalId($ID);
155
            if (! empty($parts['id']) && absint($parts['id'])) {
156
                $entity->_add_relation_to(
157
                    $parts['id'],
158
                    $relationName
159
                );
160
            }
161
        }
162
    }
163
164
165
    /**
166
     * Sets the related prices for the given ticket.
167
     *
168
     * @param EE_Ticket $entity The Ticket instance.
169
     * @param array     $prices Array of entity IDs to relate.
170
     * @throws EE_Error
171
     * @throws InvalidArgumentException
172
     * @throws InvalidDataTypeException
173
     * @throws InvalidInterfaceException
174
     * @throws ReflectionException
175
     */
176 View Code Duplication
    public static function setRelatedPrices($entity, array $prices)
177
    {
178
        $relationName = 'Price';
179
        // Remove all the existing related entities
180
        $entity->_remove_relations($relationName);
181
182
        // @todo replace loop with single query
183
        foreach ($prices as $ID) {
184
            $parts = Relay::fromGlobalId($ID);
185
            if (! empty($parts['id']) && absint($parts['id'])) {
186
                $entity->_add_relation_to(
187
                    $parts['id'],
188
                    $relationName
189
                );
190
            }
191
        }
192
    }
193
194
195
    /**
196
     * @param EE_Ticket  $ticket_entity
197
     * @param EEM_Ticket $ticket_model
198
     * @throws DomainException
199
     * @throws EE_Error
200
     * @throws InvalidArgumentException
201
     * @throws InvalidDataTypeException
202
     * @throws InvalidInterfaceException
203
     * @throws ModelConfigurationException
204
     * @throws ReflectionException
205
     * @throws RestException
206
     * @throws UnexpectedEntityException
207
     * @throws EE_Error
208
     * @since $VID:$
209
     */
210
    public static function addDefaultPrices(EE_Ticket $ticket_entity, EEM_Ticket $ticket_model)
211
    {
212
        $price_model = EEM_Price::instance();
213
        $default_prices = $price_model->get_all_default_prices();
214
        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...
215
            $default_price->save();
216
            $default_price->_add_relation_to($ticket_entity, 'Ticket');
217
        }
218
    }
219
}
220