Completed
Branch barista (01af75)
by
unknown
60:31 queued 51:15
created

EventEntityRelations::convertToGlobalId()   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\admin\events\editor;
4
5
use EE_Error;
6
use GraphQLRelay\Relay;
7
8
/**
9
 * Class EventEditorRelationalData
10
 * Retrieves relational data for entities used in the Event Editor such as dates and tickets
11
 *
12
 * @package EventEspresso\core\domain\services\admin\events\editor
13
 * @author  Manzoor Wani
14
 * @since   $VID:$
15
 */
16
class EventEntityRelations extends EventEditorData
17
{
18
19
    /**
20
     * @param array $data
21
     */
22
    private $data;
23
24
    /**
25
     * @param int $eventId
26
     * @return array
27
     * @throws EE_Error
28
     * @since $VID:$
29
     */
30
    public function getData(int $eventId)
31
    {
32
        $this->data = [
33
            'datetimes' => [],
34
            'tickets'   => [],
35
            'prices'    => [],
36
        ];
37
38
        $datetimeIds = $this->processDatetimes($eventId);
39
        $ticketIds = $this->processTickets($datetimeIds);
40
        $this->processPrices($ticketIds);
41
42
        return $this->data;
43
    }
44
45
46
    /**
47
     * @param int $eventId
48
     * @return array
49
     * @throws EE_Error
50
     * @since $VID:$
51
     */
52
    private function processDatetimes(int $eventId)
53
    {
54
        $related_models = [
55
            'tickets' => $this->ticket_model,
56
        ];
57
        // Get the IDs of event datetimes.
58
        $datetimeIds = $this->datetime_model->get_col([
59
            [ 'EVT_ID' => $eventId ],
60
            'default_where_conditions' => 'minimum',
61
        ]);
62 View Code Duplication
        foreach ($datetimeIds as $datetimeId) {
63
            $GID = $this->utilities->convertToGlobalId($this->datetime_model->item_name(), $datetimeId);
64
            foreach ($related_models as $key => $model) {
65
                // Get the IDs of related entities for the datetime ID.
66
                $Ids = $model->get_col([
67
                    [ 'Datetime.DTT_ID' => $datetimeId ],
68
                    'default_where_conditions' => 'minimum',
69
                ]);
70
                $this->data['datetimes'][ $GID ][ $key ] = ! empty($Ids)
71
                    ? $this->utilities->convertToGlobalId($model->item_name(), $Ids)
72
                    : [];
73
            }
74
        }
75
        return $datetimeIds;
76
    }
77
78
79
    /**
80
     * @param array $datetimeIds
81
     * @return array
82
     * @throws EE_Error
83
     * @since $VID:$
84
     */
85
    private function processTickets(array $datetimeIds)
86
    {
87
        $related_models = [
88
            'datetimes' => $this->datetime_model,
89
            'prices'    => $this->price_model,
90
        ];
91
        // Get the IDs of all datetime tickets.
92
        $ticketIds = $this->ticket_model->get_col([
93
            [ 'Datetime.DTT_ID' => ['IN', $datetimeIds] ],
94
            'default_where_conditions' => 'minimum',
95
        ]);
96 View Code Duplication
        foreach ($ticketIds as $ticketId) {
97
            $GID = $this->utilities->convertToGlobalId($this->ticket_model->item_name(), $ticketId);
98
99
            foreach ($related_models as $key => $model) {
100
                // Get the IDs of related entities for the ticket ID.
101
                $Ids = $model->get_col([
102
                    [ 'Ticket.TKT_ID' => $ticketId ],
103
                    'default_where_conditions' => 'minimum',
104
                ]);
105
                $this->data['tickets'][ $GID ][ $key ] = ! empty($Ids)
106
                    ? $this->utilities->convertToGlobalId($model->item_name(), $Ids)
107
                    : [];
108
            }
109
        }
110
        return $ticketIds;
111
    }
112
113
114
    /**
115
     * @param array $ticketIds
116
     * @throws EE_Error
117
     * @since $VID:$
118
     */
119
    private function processPrices(array $ticketIds)
120
    {
121
        $related_models = [
122
            'tickets'    => $this->ticket_model,
123
            'priceTypes' => $this->price_type_model,
124
        ];
125
        // Get the IDs of all ticket prices and default prices
126
        $priceIds = $this->price_model->get_col([
127
            [
128
                'OR' => [
129
                    // either the price is related to any of these tickets
130
                    'Ticket.TKT_ID' => ['IN', $ticketIds],
131
                    // or it's a default price and not trashed
132
                    'AND' => [
133
                        'PRC_deleted'    => 0,
134
                        'PRC_is_default' => 1,
135
                    ],
136
                ],
137
            ],
138
            'group_by'                 => 'PRC_ID',
139
            'default_where_conditions' => 'minimum',
140
        ]);
141 View Code Duplication
        foreach ($priceIds as $priceId) {
142
            $GID = $this->utilities->convertToGlobalId($this->price_model->item_name(), $priceId);
143
144
            foreach ($related_models as $key => $model) {
145
                // Get the IDs of related entities for the price ID.
146
                $Ids = $model->get_col([
147
                    [ 'Price.PRC_ID' => $priceId ],
148
                    'default_where_conditions' => 'minimum',
149
                ]);
150
                $this->data['prices'][ $GID ][ $key ] = ! empty($Ids)
151
                    ? $this->utilities->convertToGlobalId($model->item_name(), $Ids)
152
                    : [];
153
            }
154
        }
155
    }
156
}
157