Completed
Branch EDTR/master (cfb9e9)
by
unknown
18:30 queued 09:24
created

TicketMutation   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 120
Duplicated Lines 38.33 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 46
loc 120
rs 10
c 0
b 0
f 0
wmc 18
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareFields() 12 37 8
A setRelatedDatetimes() 17 17 4
A setRelatedPrices() 17 17 4
A addDefaultPrices() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
40
        $args = [];
41
42
        if (! empty($input['name'])) {
43
            $args['TKT_name'] = sanitize_text_field($input['name']);
44
        }
45
46
        if (! empty($input['description'])) {
47
            $args['TKT_description'] = sanitize_text_field($input['description']);
48
        }
49
50
        if (! empty($input['price'])) {
51
            $args['TKT_price'] = floatval($input['price']);
52
        }
53
54 View Code Duplication
        if (! empty($input['startDate'])) {
55
            $args['TKT_start_date'] = new DateTime(sanitize_text_field($input['startDate']));
56
        }
57
58 View Code Duplication
        if (! empty($input['endDate'])) {
59
            $args['TKT_end_date'] = new DateTime(sanitize_text_field($input['endDate']));
60
        }
61
62 View Code Duplication
        if (! empty($input['datetimes'])) {
63
            $args['datetimes'] = array_map('sanitize_text_field', (array) $input['datetimes']);
64
        }
65
66 View Code Duplication
        if (! empty($input['prices'])) {
67
            $args['prices'] = array_map('sanitize_text_field', (array) $input['prices']);
68
        }
69
70
        // Likewise the other fields...
71
72
        return $args;
73
    }
74
75
    /**
76
     * Sets the related datetimes for the given ticket.
77
     *
78
     * @param EE_Ticket $entity    The Ticket instance.
79
     * @param array     $datetimes Array of datetime IDs to relate.
80
     */
81 View Code Duplication
    public static function setRelatedDatetimes($entity, array $datetimes)
82
    {
83
        $relationName = 'Datetime';
84
        // Remove all the existing related datetimes
85
86
        $entity->_remove_relations($relationName);
87
        // @todo replace loop with single query
88
        foreach ($datetimes as $ID) {
89
            $parts = Relay::fromGlobalId($ID);
90
            if (! empty($parts['id']) && absint($parts['id'])) {
91
                $entity->_add_relation_to(
92
                    $parts['id'],
93
                    $relationName
94
                );
95
            }
96
        }
97
    }
98
99
    /**
100
     * Sets the related prices for the given ticket.
101
     *
102
     * @param EE_Ticket $entity The Ticket instance.
103
     * @param array     $prices Array of entity IDs to relate.
104
     */
105 View Code Duplication
    public static function setRelatedPrices($entity, array $prices)
106
    {
107
        $relationName = 'Price';
108
        // Remove all the existing related entities
109
        $entity->_remove_relations($relationName);
110
111
        // @todo replace loop with single query
112
        foreach ($prices as $ID) {
113
            $parts = Relay::fromGlobalId($ID);
114
            if (! empty($parts['id']) && absint($parts['id'])) {
115
                $entity->_add_relation_to(
116
                    $parts['id'],
117
                    $relationName
118
                );
119
            }
120
        }
121
    }
122
123
124
    /**
125
     * @param EE_Ticket $ticket_entity
126
     * @param EEM_Ticket $ticket_model
127
     * @throws DomainException
128
     * @throws EE_Error
129
     * @throws InvalidArgumentException
130
     * @throws InvalidDataTypeException
131
     * @throws InvalidInterfaceException
132
     * @throws ModelConfigurationException
133
     * @throws ReflectionException
134
     * @throws RestException
135
     * @throws UnexpectedEntityException
136
     * @since $VID:$
137
     */
138
    public static function addDefaultPrices(EE_Ticket $ticket_entity, EEM_Ticket $ticket_model)
139
    {
140
        $price_model = EEM_Price::instance();
141
        $default_prices = $price_model->get_all_default_prices();
142
        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...
143
            $default_price->save();
144
            $default_price->_add_relation_to($ticket_entity, 'Ticket');
145
        }
146
    }
147
}
148