Completed
Branch FET/11183/improvements-to-pue-... (dfe6b8)
by
unknown
20:21 queued 12s
created

StatsGatherer::stats()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 34
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 28
nc 1
nop 0
dl 0
loc 34
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\domain\services\pue;
3
4
use EE_Config;
5
use EE_Currency_Config;
6
use EEM_Datetime;
7
use EEM_Event;
8
use EEM_Payment_Method;
9
use EEM_Registration;
10
use EEM_Ticket;
11
use EEM_Transaction;
12
use Exception;
13
use const PHP_MINOR_VERSION;
14
use const PHP_RELEASE_VERSION;
15
16
class StatsGatherer
17
{
18
19
    const COUNT_ALL_EVENTS = 'event';
20
    const COUNT_ACTIVE_EVENTS = 'active_event';
21
    const COUNT_DATETIMES = 'datetime';
22
    const COUNT_TICKETS = 'ticket';
23
    const COUNT_DATETIMES_SOLD = 'datetime_sold';
24
    const COUNT_TICKETS_FREE = 'free_ticket';
25
    const COUNT_TICKETS_PAID = 'paid_ticket';
26
    const COUNT_TICKETS_SOLD = 'ticket_sold';
27
    const COUNT_REGISTRATIONS_APPROVED = 'registrations_approved';
28
    const COUNT_REGISTRATIONS_NOT_APPROVED = 'registrations_not_approved';
29
    const COUNT_REGISTRATIONS_PENDING = 'registrations_pending';
30
    const COUNT_REGISTRATIONS_INCOMPLETE = 'registrations_incomplete';
31
    const COUNT_REGISTRATIONS_ALL = 'registrations_all';
32
    const COUNT_REGISTRATIONS_CANCELLED = 'registrations_cancelled';
33
    const COUNT_REGISTRATIONS_DECLINED = 'registrations_declined';
34
    const SUM_TRANSACTIONS_COMPLETE_TOTAL = 'transactions_complete_total_sum';
35
    const SUM_TRANSACTIONS_ALL_PAID = 'transactions_all_paid';
36
    const INFO_SITE_CURRENCY = 'site_currency';
37
38
39
    /**
40
     * @var EEM_Payment_Method
41
     */
42
    private $payment_method_model;
43
44
45
    /**
46
     * @var EEM_Event
47
     */
48
    private $event_model;
49
50
    /**
51
     * @var EEM_Datetime
52
     */
53
    private $datetime_model;
54
55
56
    /**
57
     * @var EEM_Ticket
58
     */
59
    private $ticket_model;
60
61
62
    /**
63
     * @var EEM_Registration
64
     */
65
    private $registration_model;
66
67
68
    /**
69
     * @var EEM_Transaction
70
     */
71
    private $transaction_model;
72
73
74
    /**
75
     * @var EE_Config
76
     */
77
    private $config;
78
79
80
    /**
81
     * StatsGatherer constructor.
82
     *
83
     * @param EEM_Payment_Method $payment_method_model
84
     * @param EEM_Event          $event_model
85
     * @param EEM_Datetime       $datetime_model
86
     * @param EEM_Ticket         $ticket_model
87
     * @param EEM_Registration   $registration_model
88
     * @param EEM_Transaction    $transaction_model
89
     * @param EE_Config          $config
90
     */
91
    public function __construct(
92
        EEM_Payment_Method $payment_method_model,
93
        EEM_Event $event_model,
94
        EEM_Datetime $datetime_model,
95
        EEM_Ticket $ticket_model,
96
        EEM_Registration $registration_model,
97
        EEM_Transaction $transaction_model,
98
        EE_Config $config
99
    ) {
100
        $this->payment_method_model = $payment_method_model;
101
        $this->event_model = $event_model;
102
        $this->datetime_model = $datetime_model;
103
        $this->ticket_model = $ticket_model;
104
        $this->registration_model = $registration_model;
105
        $this->transaction_model = $transaction_model;
106
        $this->config = $config;
107
    }
108
109
110
    /**
111
     * Return the stats array for PUE UXIP stats.
112
     * @return array
113
     */
114
    public function stats()
115
    {
116
        $stats = $this->paymentMethodStats();
117
        //a-ok so let's setup our stats.
118
        $stats = array_merge($stats, array(
119
            'is_multisite' => is_multisite() && is_main_site(),
120
            'active_theme' => $this->getActiveThemeStat(),
121
            'ee4_all_events_count' => $this->getCountFor(self::COUNT_ALL_EVENTS),
122
            'ee4_active_events_count' => $this->getCountFor(self::COUNT_ACTIVE_EVENTS),
123
            'all_dtts_count' => $this->getCountFor(self::COUNT_DATETIMES),
124
            'dtt_sold' => $this->getCountFor(self::COUNT_DATETIMES_SOLD),
125
            'all_tkt_count' => $this->getCountFor(self::COUNT_TICKETS),
126
            'free_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_FREE),
127
            'paid_tkt_count' => $this->getCountFor(self::COUNT_TICKETS_PAID),
128
            'tkt_sold' => $this->getCountFor(self::COUNT_TICKETS_SOLD),
129
            'approve_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED),
130
            'pending_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING),
131
            'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED),
132
            'incomplete_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE),
133
            'cancelled_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED),
134
            'declined_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED),
135
            'all_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL),
136
            'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL),
137
            'all_transaction_paid_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID),
138
            self::INFO_SITE_CURRENCY => $this->config->currency instanceof EE_Currency_Config
139
                ? $this->config->currency->code
140
                : 'unknown',
141
            'phpversion' => implode('.', array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION))
142
        ));
143
        //remove any values that equal null.  This ensures any stats that weren't retrieved successfully are excluded.
144
        return array_filter($stats, function ($value) {
145
            return $value !== null;
146
        });
147
    }
148
149
    /**
150
     * @param string $which enum (@see constants prefixed with COUNT)
151
     * @return int|null
152
     */
153
    private function getCountFor($which)
154
    {
155
        try {
156
            switch ($which) {
157
                case self::COUNT_ALL_EVENTS:
158
                    $count = $this->event_model->count();
159
                    break;
160
                case self::COUNT_TICKETS:
161
                    $count = $this->ticket_model->count();
162
                    break;
163
                case self::COUNT_DATETIMES:
164
                    $count = $this->datetime_model->count();
165
                    break;
166
                case self::COUNT_ACTIVE_EVENTS:
167
                    $count = $this->event_model->get_active_events(array(), true);
168
                    break;
169
                case self::COUNT_DATETIMES_SOLD:
170
                    $count = $this->datetime_model->sum(array(), 'DTT_sold');
171
                    break;
172
                case self::COUNT_TICKETS_FREE:
173
                    $count = $this->ticket_model->count(array(array(
174
                        'TKT_price' => 0
175
                    )));
176
                    break;
177
                case self::COUNT_TICKETS_PAID:
178
                    $count = $this->ticket_model->count(array(array(
179
                        'TKT_price' => array('>', 0)
180
                    )));
181
                    break;
182
                case self::COUNT_TICKETS_SOLD:
183
                    $count = $this->ticket_model->sum(array(), 'TKT_sold');
184
                    break;
185
                case self::COUNT_REGISTRATIONS_ALL:
186
                    $count = $this->registration_model->count();
187
                    break;
188 View Code Duplication
                case self::COUNT_REGISTRATIONS_CANCELLED:
189
                    $count = $this->registration_model->count(
190
                        array(
191
                            array(
192
                                'STS_ID' => EEM_Registration::status_id_cancelled
193
                            )
194
                        )
195
                    );
196
                    break;
197 View Code Duplication
                case self::COUNT_REGISTRATIONS_INCOMPLETE:
198
                    $count = $this->registration_model->count(
199
                        array(
200
                            array(
201
                                'STS_ID' => EEM_Registration::status_id_incomplete
202
                            )
203
                        )
204
                    );
205
                    break;
206 View Code Duplication
                case self::COUNT_REGISTRATIONS_NOT_APPROVED:
207
                    $count = $this->registration_model->count(
208
                        array(
209
                            array(
210
                                'STS_ID' => EEM_Registration::status_id_not_approved
211
                            )
212
                        )
213
                    );
214
                    break;
215 View Code Duplication
                case self::COUNT_REGISTRATIONS_DECLINED:
216
                    $count = $this->registration_model->count(
217
                        array(
218
                            array(
219
                                'STS_ID' => EEM_Registration::status_id_declined
220
                            )
221
                        )
222
                    );
223
                    break;
224 View Code Duplication
                case self::COUNT_REGISTRATIONS_PENDING:
225
                    $count = $this->registration_model->count(
226
                        array(
227
                            array(
228
                                'STS_ID' => EEM_Registration::status_id_pending_payment
229
                            )
230
                        )
231
                    );
232
                    break;
233 View Code Duplication
                case self::COUNT_REGISTRATIONS_APPROVED:
234
                    $count = $this->registration_model->count(
235
                        array(
236
                            array(
237
                                'STS_ID' => EEM_Registration::status_id_approved
238
                            )
239
                        )
240
                    );
241
                    break;
242
                case self::SUM_TRANSACTIONS_COMPLETE_TOTAL:
243
                    $count = $this->transaction_model->sum(
244
                        array(
245
                            array(
246
                                'STS_ID' => EEM_Transaction::complete_status_code
247
                            )
248
                        ),
249
                        'TXN_total'
250
                    );
251
                    break;
252
                case self::SUM_TRANSACTIONS_ALL_PAID:
253
                    $count = $this->transaction_model->sum(
254
                        array(),
255
                        'TXN_paid'
256
                    );
257
                    break;
258
                default:
259
                    $count = null;
260
                    break;
261
            }
262
        } catch (Exception $e) {
263
            $count = null;
264
        }
265
        return $count;
266
    }
267
268
    /**
269
     * Return the active theme.
270
     * @return false|string
271
     */
272
    private function getActiveThemeStat()
273
    {
274
        $theme = wp_get_theme();
275
        return $theme->get('Name');
276
    }
277
278
    /**
279
     * @return array
280
     */
281
    private function paymentMethodStats()
282
    {
283
        $payment_method_stats = array();
284
        try {
285
            $active_payment_methods = $this->payment_method_model->get_all_active(
286
                null,
287
                array('group_by' => 'PMD_type')
288
            );
289
            if ($active_payment_methods) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $active_payment_methods of type EE_Base_Class[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
290
                foreach ($active_payment_methods as $payment_method) {
291
                    $payment_method_stats[$payment_method->name() . '_active_payment_method'] = 1;
292
                }
293
            }
294
        } catch (Exception $e) {
295
            //do nothing just prevents fatals.
296
        }
297
        return $payment_method_stats;
298
    }
299
}