Completed
Branch EDTR/refactor-master (6f4a48)
by
unknown
19:30 queued 10:54
created

StatsGatherer::getActiveAddons()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\pue;
4
5
use EE_Config;
6
use EE_Currency_Config;
7
use EE_Registry;
8
use EEM_Datetime;
9
use EEM_Event;
10
use EEM_Payment_Method;
11
use EEM_Registration;
12
use EEM_Ticket;
13
use EEM_Transaction;
14
use Exception;
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
     *
113
     * @return array
114
     */
115
    public function stats()
116
    {
117
        global $wp_version;
118
        $stats = $this->paymentMethodStats();
119
        // a-ok so let's setup our stats.
120
        $stats = array_merge($stats, array(
121
            'is_multisite'                    => is_multisite() && is_main_site(),
122
            'active_theme'                    => $this->getActiveThemeStat(),
123
            'ee4_all_events_count'            => $this->getCountFor(self::COUNT_ALL_EVENTS),
124
            'ee4_active_events_count'         => $this->getCountFor(self::COUNT_ACTIVE_EVENTS),
125
            'all_dtts_count'                  => $this->getCountFor(self::COUNT_DATETIMES),
126
            'dtt_sold'                        => $this->getCountFor(self::COUNT_DATETIMES_SOLD),
127
            'all_tkt_count'                   => $this->getCountFor(self::COUNT_TICKETS),
128
            'free_tkt_count'                  => $this->getCountFor(self::COUNT_TICKETS_FREE),
129
            'paid_tkt_count'                  => $this->getCountFor(self::COUNT_TICKETS_PAID),
130
            'tkt_sold'                        => $this->getCountFor(self::COUNT_TICKETS_SOLD),
131
            'approve_registration_count'      => $this->getCountFor(self::COUNT_REGISTRATIONS_APPROVED),
132
            'pending_registration_count'      => $this->getCountFor(self::COUNT_REGISTRATIONS_PENDING),
133
            'not_approved_registration_count' => $this->getCountFor(self::COUNT_REGISTRATIONS_NOT_APPROVED),
134
            'incomplete_registration_count'   => $this->getCountFor(self::COUNT_REGISTRATIONS_INCOMPLETE),
135
            'cancelled_registration_count'    => $this->getCountFor(self::COUNT_REGISTRATIONS_CANCELLED),
136
            'declined_registration_count'     => $this->getCountFor(self::COUNT_REGISTRATIONS_DECLINED),
137
            'all_registration_count'          => $this->getCountFor(self::COUNT_REGISTRATIONS_ALL),
138
            'completed_transaction_total_sum' => $this->getCountFor(self::SUM_TRANSACTIONS_COMPLETE_TOTAL),
139
            'all_transaction_paid_sum'        => $this->getCountFor(self::SUM_TRANSACTIONS_ALL_PAID),
140
            self::INFO_SITE_CURRENCY          => $this->config->currency instanceof EE_Currency_Config
141
                ? $this->config->currency->code
142
                : 'unknown',
143
            'phpversion'                      => implode(
144
                '.',
145
                array(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION)
146
            ),
147
            'wpversion'                       => $wp_version,
148
            'active_addons'                   => $this->getActiveAddons(),
149
        ));
150
        // remove any values that equal null.  This ensures any stats that weren't retrieved successfully are excluded.
151
        return array_filter($stats, function ($value) {
152
            return $value !== null;
153
        });
154
    }
155
156
    /**
157
     * @param string $which enum (@see constants prefixed with COUNT)
158
     * @return int|null
159
     */
160
    private function getCountFor($which)
161
    {
162
        try {
163
            switch ($which) {
164
                case self::COUNT_ALL_EVENTS:
165
                    $count = $this->event_model->count();
166
                    break;
167
                case self::COUNT_TICKETS:
168
                    $count = $this->ticket_model->count();
169
                    break;
170
                case self::COUNT_DATETIMES:
171
                    $count = $this->datetime_model->count();
172
                    break;
173
                case self::COUNT_ACTIVE_EVENTS:
174
                    $count = $this->event_model->get_active_events(array(), true);
175
                    break;
176
                case self::COUNT_DATETIMES_SOLD:
177
                    $count = $this->datetime_model->sum(array(), 'DTT_sold');
178
                    break;
179
                case self::COUNT_TICKETS_FREE:
180
                    $count = $this->ticket_model->count(array(
181
                        array(
182
                            'TKT_price' => 0,
183
                        ),
184
                    ));
185
                    break;
186
                case self::COUNT_TICKETS_PAID:
187
                    $count = $this->ticket_model->count(array(
188
                        array(
189
                            'TKT_price' => array('>', 0),
190
                        ),
191
                    ));
192
                    break;
193
                case self::COUNT_TICKETS_SOLD:
194
                    $count = $this->ticket_model->sum(array(), 'TKT_sold');
195
                    break;
196
                case self::COUNT_REGISTRATIONS_ALL:
197
                    $count = $this->registration_model->count();
198
                    break;
199 View Code Duplication
                case self::COUNT_REGISTRATIONS_CANCELLED:
200
                    $count = $this->registration_model->count(
201
                        array(
202
                            array(
203
                                'STS_ID' => EEM_Registration::status_id_cancelled,
204
                            ),
205
                        )
206
                    );
207
                    break;
208 View Code Duplication
                case self::COUNT_REGISTRATIONS_INCOMPLETE:
209
                    $count = $this->registration_model->count(
210
                        array(
211
                            array(
212
                                'STS_ID' => EEM_Registration::status_id_incomplete,
213
                            ),
214
                        )
215
                    );
216
                    break;
217 View Code Duplication
                case self::COUNT_REGISTRATIONS_NOT_APPROVED:
218
                    $count = $this->registration_model->count(
219
                        array(
220
                            array(
221
                                'STS_ID' => EEM_Registration::status_id_not_approved,
222
                            ),
223
                        )
224
                    );
225
                    break;
226 View Code Duplication
                case self::COUNT_REGISTRATIONS_DECLINED:
227
                    $count = $this->registration_model->count(
228
                        array(
229
                            array(
230
                                'STS_ID' => EEM_Registration::status_id_declined,
231
                            ),
232
                        )
233
                    );
234
                    break;
235 View Code Duplication
                case self::COUNT_REGISTRATIONS_PENDING:
236
                    $count = $this->registration_model->count(
237
                        array(
238
                            array(
239
                                'STS_ID' => EEM_Registration::status_id_pending_payment,
240
                            ),
241
                        )
242
                    );
243
                    break;
244 View Code Duplication
                case self::COUNT_REGISTRATIONS_APPROVED:
245
                    $count = $this->registration_model->count(
246
                        array(
247
                            array(
248
                                'STS_ID' => EEM_Registration::status_id_approved,
249
                            ),
250
                        )
251
                    );
252
                    break;
253
                case self::SUM_TRANSACTIONS_COMPLETE_TOTAL:
254
                    $count = $this->transaction_model->sum(
255
                        array(
256
                            array(
257
                                'STS_ID' => EEM_Transaction::complete_status_code,
258
                            ),
259
                        ),
260
                        'TXN_total'
261
                    );
262
                    break;
263
                case self::SUM_TRANSACTIONS_ALL_PAID:
264
                    $count = $this->transaction_model->sum(
265
                        array(),
266
                        'TXN_paid'
267
                    );
268
                    break;
269
                default:
270
                    $count = null;
271
                    break;
272
            }
273
        } catch (Exception $e) {
274
            $count = null;
275
        }
276
        return $count;
277
    }
278
279
    /**
280
     * Return the active theme.
281
     *
282
     * @return false|string
283
     */
284
    private function getActiveThemeStat()
285
    {
286
        $theme = wp_get_theme();
287
        return $theme->get('Name');
288
    }
289
290
    /**
291
     * @return array
292
     */
293
    private function paymentMethodStats()
294
    {
295
        $payment_method_stats = array();
296
        try {
297
            $active_payment_methods = $this->payment_method_model->get_all_active(
298
                null,
299
                array('group_by' => 'PMD_type')
300
            );
301
            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...
302
                foreach ($active_payment_methods as $payment_method) {
303
                    $payment_method_stats[ $payment_method->name() . '_active_payment_method' ] = 1;
304
                }
305
            }
306
        } catch (Exception $e) {
307
            // do nothing just prevents fatals.
308
        }
309
        return $payment_method_stats;
310
    }
311
312
313
    /**
314
     * Return a list of active EE add-ons and their versions.
315
     *
316
     * @return string
317
     */
318
    private function getActiveAddons()
319
    {
320
        $activeAddons = [];
321
        $addOns = EE_Registry::instance()->addons;
322
        if (! empty($addOns)) {
323
            foreach ($addOns as $addon) {
324
                $activeAddons[] = $addon->name() . '@' . $addon->version();
325
            }
326
        }
327
        return implode(',', $activeAddons);
328
    }
329
}
330