Completed
Branch EDTR/master (5c103b)
by
unknown
10:13 queued 38s
created

EventEntityRelations::processPrices()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 23
loc 23
rs 9.552
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
 * Description
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($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($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
        foreach ($datetimeIds as $datetimeId) {
63
            $GID = $this->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->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 View Code Duplication
    private function processTickets(array $datetimeIds)
86
    {
87
88
        $related_models = [
89
            'datetimes' => $this->datetime_model,
90
            'prices'    => $this->price_model,
91
        ];
92
        // Get the IDs of all datetime tickets.
93
        $ticketIds = $this->ticket_model->get_col([
94
            [ 'Datetime.DTT_ID' => ['IN', $datetimeIds] ],
95
            'default_where_conditions' => 'minimum',
96
        ]);
97
        foreach ($ticketIds as $ticketId) {
98
            $GID = $this->convertToGlobalId($this->ticket_model->item_name(), $ticketId);
99
100
            foreach ($related_models as $key => $model) {
101
                // Get the IDs of related entities for the ticket ID.
102
                $Ids = $model->get_col([
103
                    [ 'Ticket.TKT_ID' => $ticketId ],
104
                    'default_where_conditions' => 'minimum',
105
                ]);
106
                $this->data['tickets'][ $GID ][ $key ] = ! empty($Ids)
107
                    ? $this->convertToGlobalId($model->item_name(), $Ids)
108
                    : [];
109
            }
110
        }
111
        return $ticketIds;
112
    }
113
114
115
    /**
116
     * @param array $ticketIds
117
     * @throws EE_Error
118
     * @since $VID:$
119
     */
120 View Code Duplication
    private function processPrices(array $ticketIds)
121
    {
122
        $related_models = [
123
            'tickets'    => $this->ticket_model,
124
            'priceTypes' => $this->price_type_model,
125
        ];
126
        // Get the IDs of all ticket prices.
127
        $priceIds = $this->price_model->get_col([['Ticket.TKT_ID' => ['IN', $ticketIds]]]);
128
        foreach ($priceIds as $priceId) {
129
            $GID = $this->convertToGlobalId($this->price_model->item_name(), $priceId);
130
131
            foreach ($related_models as $key => $model) {
132
                // Get the IDs of related entities for the price ID.
133
                $Ids = $model->get_col([
134
                    [ 'Price.PRC_ID' => $priceId ],
135
                    'default_where_conditions' => 'minimum',
136
                ]);
137
                $this->data['prices'][ $GID ][ $key ] = ! empty($Ids)
138
                    ? $this->convertToGlobalId($model->item_name(), $Ids)
139
                    : [];
140
            }
141
        }
142
    }
143
144
145
    /**
146
     * Convert the DB ID into GID
147
     *
148
     * @param string    $type
149
     * @param int|int[] $ID
150
     * @return mixed
151
     */
152
    public function convertToGlobalId($type, $ID)
153
    {
154
        $convertToGlobalId = [$this, 'convertToGlobalId'];
155
        if (is_array($ID)) {
156
            return array_map(
157
                static function ($id) use ($convertToGlobalId, $type) {
158
                    return $convertToGlobalId($type, $id);
159
                },
160
                $ID
161
            );
162
        }
163
        return Relay::toGlobalId($type, $ID);
164
    }
165
}
166