Completed
Branch BUG/double-ampersand-in-regist... (7dce02)
by
unknown
131:59 queued 62:17
created

Datetime::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\libraries\rest_api\calculations;
4
5
use EE_Error;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
use EventEspresso\core\exceptions\InvalidInterfaceException;
8
use EventEspresso\core\libraries\rest_api\calculations\Base as DatetimeCalculationBase;
9
use EventEspresso\core\libraries\rest_api\controllers\model\Base as DatetimeControllerBase;
10
use EEM_Datetime;
11
use EEM_Registration;
12
use EE_Datetime;
13
use EventEspresso\core\libraries\rest_api\RestException;
14
use InvalidArgumentException;
15
use ReflectionException;
16
use WP_REST_Request;
17
18
class Datetime extends DatetimeCalculationBase
19
{
20
    /**
21
     * @var EEM_Datetime
22
     */
23
    protected $datetime_model;
24
25
    /**
26
     * @var EEM_Registration
27
     */
28
    protected $registration_model;
29
    public function __construct(EEM_Datetime $datetime_model, EEM_Registration $registration_model)
30
    {
31
        $this->datetime_model = $datetime_model;
32
        $this->registration_model = $registration_model;
33
    }
34
35
    /**
36
     * Calculates the total spaces available on the datetime, taking into account
37
     * ticket limits too.
38
     *
39
     * @see EE_Datetime::spaces_remaining( true )
40
     * @param array            $wpdb_row
41
     * @param WP_REST_Request $request
42
     * @param DatetimeControllerBase  $controller
43
     * @return int
44
     * @throws EE_Error
45
     * @throws InvalidDataTypeException
46
     * @throws InvalidInterfaceException
47
     * @throws InvalidArgumentException
48
     * @throws ReflectionException
49
     */
50
    public function spacesRemainingConsideringTickets($wpdb_row, $request, $controller)
51
    {
52
        if (is_array($wpdb_row) && isset($wpdb_row['Datetime.DTT_ID'])) {
53
            $dtt_obj = $this->datetime_model->get_one_by_ID($wpdb_row['Datetime.DTT_ID']);
54
        } else {
55
            $dtt_obj = null;
56
        }
57
        if ($dtt_obj instanceof EE_Datetime) {
58
            return $dtt_obj->spaces_remaining(true);
59
        }
60
        throw new EE_Error(
61
            sprintf(
62
                __(
63
                // @codingStandardsIgnoreStart
64
                    'Cannot calculate spaces_remaining_considering_tickets because the datetime with ID %1$s (from database row %2$s) was not found',
65
                    // @codingStandardsIgnoreEnd
66
                    'event_espresso'
67
                ),
68
                $wpdb_row['Datetime.DTT_ID'],
69
                print_r($wpdb_row, true)
70
            )
71
        );
72
    }
73
74
75
    /**
76
     * Counts registrations who have checked into this datetime
77
     *
78
     * @param array           $wpdb_row
79
     * @param WP_REST_Request $request
80
     * @param DatetimeControllerBase $controller
81
     * @return int
82
     * @throws EE_Error
83
     * @throws InvalidArgumentException
84
     * @throws InvalidDataTypeException
85
     * @throws InvalidInterfaceException
86
     * @throws RestException
87
     */
88 View Code Duplication
    public function registrationsCheckedInCount($wpdb_row, $request, $controller)
89
    {
90
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) {
91
            throw new EE_Error(
92
                sprintf(
93
                    __(
94
                    // @codingStandardsIgnoreStart
95
                        'Cannot calculate registrations_checked_in_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"',
96
                        // @codingStandardsIgnoreEnd
97
                        'event_espresso'
98
                    ),
99
                    print_r($wpdb_row, true)
100
                )
101
            );
102
        }
103
        $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_in_count');
104
        return $this->registration_model
105
                               ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], true);
106
    }
107
108
109
    /**
110
     * Counts registrations who have checked out of this datetime
111
     *
112
     * @param array           $wpdb_row
113
     * @param WP_REST_Request $request
114
     * @param DatetimeControllerBase $controller
115
     * @return int
116
     * @throws EE_Error
117
     * @throws InvalidArgumentException
118
     * @throws InvalidDataTypeException
119
     * @throws InvalidInterfaceException
120
     * @throws RestException
121
     */
122 View Code Duplication
    public function registrationsCheckedOutCount($wpdb_row, $request, $controller)
123
    {
124
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) {
125
            throw new EE_Error(
126
                sprintf(
127
                    __(
128
                    // @codingStandardsIgnoreStart
129
                        'Cannot calculate registrations_checked_out_count because the database row %1$s does not have an entry for "Datetime.DTT_ID"',
130
                        // @codingStandardsIgnoreEnd
131
                        'event_espresso'
132
                    ),
133
                    print_r($wpdb_row, true)
134
                )
135
            );
136
        }
137
        $this->verifyCurrentUserCan('ee_read_checkins', 'registrations_checked_out_count');
138
        return $this->registration_model
139
                               ->count_registrations_checked_into_datetime($wpdb_row['Datetime.DTT_ID'], false);
140
    }
141
142
143
    /**
144
     * Counts the number of pending-payment registrations for this event (regardless
145
     * of how many datetimes each registrations' ticket purchase is for)
146
     *
147
     * @param array           $wpdb_row
148
     * @param WP_REST_Request $request
149
     * @param DatetimeControllerBase $controller
150
     * @return int
151
     * @throws EE_Error
152
     * @throws InvalidArgumentException
153
     * @throws InvalidDataTypeException
154
     * @throws InvalidInterfaceException
155
     * @throws RestException
156
     */
157 View Code Duplication
    public function spotsTakenPendingPayment($wpdb_row, $request, $controller)
158
    {
159
        if (! is_array($wpdb_row) || ! isset($wpdb_row['Datetime.DTT_ID'])) {
160
            throw new EE_Error(
161
                sprintf(
162
                    __(
163
                    // @codingStandardsIgnoreStart
164
                        'Cannot calculate spots_taken_pending_payment because the database row %1$s does not have an entry for "Datetime.DTT_ID"',
165
                        // @codingStandardsIgnoreEnd
166
                        'event_espresso'
167
                    ),
168
                    print_r($wpdb_row, true)
169
                )
170
            );
171
        }
172
        $this->verifyCurrentUserCan('ee_read_registrations', 'spots_taken_pending_payment');
173
        return $this->registration_model->count(
174
            array(
175
                array(
176
                    'Ticket.Datetime.DTT_ID' => $wpdb_row['Datetime.DTT_ID'],
177
                    'STS_ID'                 => EEM_Registration::status_id_pending_payment,
178
                ),
179
            ),
180
            'REG_ID',
181
            true
182
        );
183
    }
184
185
186
    /**
187
     * Provides an array for all the calculations possible that outlines a json schema for those calculations.
188
     * Array is indexed by calculation (snake case) and value is the schema for that calculation.
189
     *
190
     * @since $VID:$
191
     * @return array
192
     */
193
    public function schemaForCalculations()
194
    {
195
        return array(
196
            'spaces_remaining_considering_tickets' => array(
197
                'description' => esc_html__(
198
                    'Calculates the total spaces available on the datetime, taking into account ticket limits too.',
199
                    'event_espresso'
200
                ),
201
                'type' => 'number'
202
            ),
203
            'registrations_checked_in_count' => array(
204
                'description' => esc_html__(
205
                    'Counts registrations who have checked into this datetime.',
206
                    'event_espresso'
207
                ),
208
                'type' => 'number'
209
            ),
210
            'registrations_checked_out_count' => array(
211
                'description' => esc_html__(
212
                    'Counts registrations who have checked out of this datetime.',
213
                    'event_espresso'
214
                ),
215
                'type' => 'number'
216
            ),
217
            'spots_taken_pending_payment' => array(
218
                'description' => esc_html__(
219
                    'The count of pending-payment registrations for this event (regardless of how many datetimes each registration\'s ticket purchase is for',
220
                    'event_espresso'
221
                ),
222
                'type' => 'number'
223
            ),
224
        );
225
    }
226
}
227