Completed
Branch EDTR/refactor-master (e18acd)
by
unknown
11:03 queued 02:08
created

AdvancedEditorEntityData::getAllEventData()   F

Complexity

Conditions 19
Paths 17

Size

Total Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
nc 17
nop 1
dl 0
loc 111
rs 3.6133
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\admin\events\editor;
4
5
use DomainException;
6
use EE_Admin_Config;
7
use EE_Error;
8
use EEM_Datetime;
9
use EEM_Event;
10
use EEM_Price;
11
use EEM_Price_Type;
12
use EEM_Ticket;
13
use EEM_Venue;
14
use EventEspresso\core\domain\services\assets\EspressoEditorAssetManager;
15
use EventEspresso\core\domain\services\converters\RestApiSpoofer;
16
use EventEspresso\core\exceptions\InvalidDataTypeException;
17
use EventEspresso\core\exceptions\InvalidInterfaceException;
18
use EventEspresso\core\exceptions\ModelConfigurationException;
19
use EventEspresso\core\exceptions\RestPasswordIncorrectException;
20
use EventEspresso\core\exceptions\RestPasswordRequiredException;
21
use EventEspresso\core\exceptions\UnexpectedEntityException;
22
use EventEspresso\core\libraries\rest_api\RestException;
23
use InvalidArgumentException;
24
use ReflectionException;
25
use WP_Post;
26
27
/**
28
 * Class AdvancedEditorEntityData
29
 * Description
30
 *
31
 * @package EventEspresso\core\domain\services\admin\events\editor
32
 * @author  Brent Christensen
33
 * @since   $VID:$
34
 */
35
class AdvancedEditorEntityData
36
{
37
38
    /**
39
     * @var RestApiSpoofer
40
     */
41
    protected $spoofer;
42
43
    /**
44
     * @var EE_Admin_Config
45
     */
46
    protected $admin_config;
47
48
    /**
49
     * @var EEM_Datetime $datetime_model
50
     */
51
    protected $datetime_model;
52
53
    /**
54
     * @var EEM_Event $event_model
55
     */
56
    protected $event_model;
57
58
    /**
59
     * @var EEM_Price $price_model
60
     */
61
    protected $price_model;
62
63
    /**
64
     * @var EEM_Price_Type $price_type_model
65
     */
66
    protected $price_type_model;
67
68
    /**
69
     * @var EEM_Ticket $ticket_model
70
     */
71
    protected $ticket_model;
72
    /**
73
     * @var EEM_Venue $venue_model
74
     */
75
    protected $venue_model;
76
77
78
    /**
79
     * AdvancedEditorAdminForm constructor.
80
     *
81
     * @param RestApiSpoofer $spoofer
82
     * @param EE_Admin_Config $admin_config
83
     * @param EEM_Datetime $datetime_model
84
     * @param EEM_Event $event_model
85
     * @param EEM_Price $price_model
86
     * @param EEM_Price_Type $price_type_model
87
     * @param EEM_Ticket $ticket_model
88
     * @param EEM_Venue $venue_model
89
     */
90
    public function __construct(
91
        RestApiSpoofer $spoofer,
92
        EE_Admin_Config $admin_config,
93
        EEM_Datetime $datetime_model,
94
        EEM_Event $event_model,
95
        EEM_Price $price_model,
96
        EEM_Price_Type $price_type_model,
97
        EEM_Ticket $ticket_model,
98
        EEM_Venue $venue_model
99
    ) {
100
        $this->admin_config = $admin_config;
101
        $this->spoofer = $spoofer;
102
        $this->datetime_model = $datetime_model;
103
        $this->event_model = $event_model;
104
        $this->price_model = $price_model;
105
        $this->price_type_model = $price_type_model;
106
        $this->ticket_model = $ticket_model;
107
        $this->venue_model = $venue_model;
108
        add_action('admin_enqueue_scripts', [$this, 'loadScriptsStyles']);
109
    }
110
111
112
    /**
113
     * @throws EE_Error
114
     * @throws InvalidArgumentException
115
     * @throws InvalidDataTypeException
116
     * @throws InvalidInterfaceException
117
     * @throws ModelConfigurationException
118
     * @throws ReflectionException
119
     * @throws RestException
120
     * @throws RestPasswordIncorrectException
121
     * @throws RestPasswordRequiredException
122
     * @throws UnexpectedEntityException
123
     * @throws DomainException
124
     * @since $VID:$
125
     */
126
    public function loadScriptsStyles()
127
    {
128
        if ($this->admin_config->useAdvancedEditor()) {
129
            global $post;
130
            $eventId = isset($_REQUEST['post']) ? absint($_REQUEST['post']) : 0;
131
            $eventId = $eventId === 0 && $post instanceof WP_Post ? $post->ID : $eventId;
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
132
            if ($eventId) {
133
                $data = $this->getAllEventData($eventId);
134
                $data = wp_json_encode($data);
135
                add_action(
136
                    'admin_footer',
137
                    static function () use ($data) {
138
                        wp_add_inline_script(
139
                            EspressoEditorAssetManager::JS_HANDLE_EDITOR,
140
                            "var eeEditorEventData={$data};",
141
                            'before'
142
                        );
143
                    }
144
                );
145
            }
146
        }
147
    }
148
149
150
    /**
151
     * @param int $eventId
152
     * @return array
153
     * @throws EE_Error
154
     * @throws InvalidDataTypeException
155
     * @throws InvalidInterfaceException
156
     * @throws ModelConfigurationException
157
     * @throws RestPasswordIncorrectException
158
     * @throws RestPasswordRequiredException
159
     * @throws UnexpectedEntityException
160
     * @throws RestException
161
     * @throws InvalidArgumentException
162
     * @throws ReflectionException
163
     * @throws DomainException
164
     * @since $VID:$
165
     */
166
    protected function getAllEventData($eventId)
167
    {
168
        // these should ultimately be extracted out into their own classes (one per model)
169
        $event = $this->spoofer->getOneApiResult(
170
            $this->event_model,
171
            [['EVT_ID' => $eventId]]
172
        );
173
        if (! (is_array($event) && isset($event['EVT_ID']) && $event['EVT_ID'] === $eventId)) {
174
            return [];
175
        }
176
        $event = [$eventId => $event];
177
        $relations = [
178
            'event' => [ $eventId => [] ],
179
            'datetime' => [],
180
            'ticket' => [],
181
            'price' => [],
182
        ];
183
        $eventDates = $this->spoofer->getApiResults(
184
            $this->datetime_model,
185
            [[
186
                'EVT_ID' => $eventId,
187
                'DTT_deleted' => ['IN', [true, false]]
188
            ]]
189
        );
190
        $relations['event'][ $eventId ]['datetime'] = [];
191
192
        $datetimes = [];
193
        $eventDateTickets = [];
194
        if (is_array($eventDates)){
195
            foreach ($eventDates as $eventDate) {
196
                if (isset($eventDate['DTT_ID']) && $eventDate['DTT_ID']) {
197
                    $DTT_ID = $eventDate['DTT_ID'];
198
                    $datetimes[ $DTT_ID ] = $eventDate;
199
                    $relations['event'][ $eventId ]['datetime'][] = $DTT_ID;
200
                    $eventDateTickets[ $DTT_ID ] = $this->spoofer->getApiResults(
201
                        $this->ticket_model,
202
                        [[
203
                            'Datetime.DTT_ID' => $DTT_ID,
204
                            'TKT_deleted' => ['IN', [true, false]]
205
                        ]]
206
                    );
207
                }
208
            }
209
        }
210
211
        $prices = [];
212
        $tickets = [];
213
        if (is_array($eventDateTickets)) {
214
            foreach ($eventDateTickets as $DTT_ID => $dateTickets) {
215
                if (is_array($dateTickets)) {
216
                    $relations['datetime'][ $DTT_ID ]['ticket'] = [];
217
                    foreach ($dateTickets as $ticket) {
218
                        if (isset($ticket['TKT_ID']) && $ticket['TKT_ID']) {
219
                            $TKT_ID = $ticket['TKT_ID'];
220
                            $tickets[ $TKT_ID ] = $ticket;
221
                            $relations['datetime'][ $DTT_ID ]['ticket'][] = $TKT_ID;
222
                            $ticketPrices[ $TKT_ID ] = $this->spoofer->getApiResults(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ticketPrices was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ticketPrices = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
223
                                $this->price_model,
224
                                [['Ticket.TKT_ID' => $TKT_ID]]
225
                            );
226
                            if (is_array($ticketPrices[ $TKT_ID ])) {
227
                                $relations['ticket'][ $TKT_ID ]['price'] = [];
228
                                foreach ($ticketPrices[ $TKT_ID ] as $ticketPrice) {
229
                                    $PRC_ID = $ticketPrice['PRC_ID'];
230
                                    $prices[ $PRC_ID ] = $ticketPrice;
231
                                    $relations['ticket'][ $TKT_ID ]['price'][] = $PRC_ID;
232
                                }
233
                            }
234
                        }
235
                    }
236
                }
237
            }
238
        }
239
        $price_type_results = $this->spoofer->getApiResults(
240
            $this->price_type_model,
241
            [['PRT_deleted' => false]]
242
        );
243
        $price_types = [];
244
        foreach ($price_type_results as $price_type) {
245
            $price_types[ $price_type['PRT_ID'] ] = $price_type;
246
        }
247
        $venue = $this->spoofer->getOneApiResult(
248
            $this->venue_model,
249
            [['Event.EVT_ID' => $eventId]]
250
        );
251
        if (is_array($venue) && isset($venue['VNU_ID'])) {
252
            $relations['event'][ $eventId ]['venue'] = [ $venue['VNU_ID'] ];
253
            $venue = [$venue['VNU_ID'] => $venue];
254
        }
255
256
        $schemas = [
257
            'event'      => $this->spoofer->getModelSchema('events'),
258
            'datetime'   => $this->spoofer->getModelSchema('datetimes'),
259
            'ticket'     => $this->spoofer->getModelSchema('tickets'),
260
            'price'      => $this->spoofer->getModelSchema('prices'),
261
            'price_type' => $this->spoofer->getModelSchema('price_types'),
262
            'venue'      => $this->spoofer->getModelSchema('venues'),
263
        ];
264
265
        return [
266
            'eventId'    => $eventId,
267
            'event'      => $event,
268
            'datetime'   => $datetimes,
269
            'ticket'     => $tickets,
270
            'price'      => $prices,
271
            'price_type' => $price_types,
272
            'venue'      => $venue,
273
            'schemas'    => $schemas,
274
            'relations'  => $relations,
275
        ];
276
    }
277
}
278