Completed
Branch BUG/11268/session-ticket-relea... (393c4a)
by
unknown
40:56 queued 25:34
created

StatsGatherer::getCountFor()   D

Complexity

Conditions 19
Paths 35

Size

Total Lines 114
Code Lines 86

Duplication

Lines 54
Ratio 47.37 %

Importance

Changes 0
Metric Value
cc 19
eloc 86
nc 35
nop 1
dl 54
loc 114
rs 4.764
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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