Passed
Push — master ( 01a84c...98164f )
by Paweł
03:03
created

AccountStatsManager::getDailyStatsData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 03.04.2018
6
 */
7
8
namespace app\modules\admin\components;
9
10
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
use yii\db\Expression;
14
15
class AccountStatsManager extends Component
16
{
17
    /**
18
     * @var \app\modules\admin\models\Account
19
     */
20
    public $account;
21
22
    /**
23
     * Daily stats from last month.
24
     *
25
     * @var \app\models\AccountStats[]
26
     */
27
    protected $dailyStats;
28
29
    /**
30
     * Monthly stats from last year.
31
     *
32
     * @var \app\models\AccountStats[]
33
     */
34
    protected $monthlyStats;
35
36
    public function init()
37
    {
38
        parent::init();
39
        if (!$this->account) {
40
            throw new InvalidConfigException('Property \'account\' can not be empty.');
41
        }
42
    }
43
44
    public function lastChange($attribute)
45
    {
46
        $statsData = $this->getDailyStatsData();
47
        if (!$statsData) {
48
            return null;
49
        }
50
        $latest = array_shift($statsData);
51
        $oldest = array_shift($statsData);
52
53
        return $latest->$attribute - $oldest->$attribute;
54
    }
55
56
    public function lastStatsFrom()
57
    {
58
        $statsData = $this->getDailyStatsData();
59
        if (!$statsData) {
60
            return null;
61
        }
62
        array_shift($statsData);
63
        $oldest = array_shift($statsData);
64
65
        return $oldest->created_at;
66
    }
67
68
    public function lastMonthChange($attribute)
69
    {
70
        $statsData = $this->getDailyStatsData();
71
        if (!$statsData) {
72
            return null;
73
        }
74
75
        $latest = array_shift($statsData);
76
        $oldest = end($statsData);
77
78
        return $latest->$attribute - $oldest->$attribute;
79
    }
80
81
    public function dailyStatsFrom()
82
    {
83
        $statsData = $this->getDailyStatsData();
84
        if (!$statsData) {
85
            return null;
86
        }
87
        array_shift($statsData);
88
        $oldest = end($statsData);
89
90
        return $oldest->created_at;
91
    }
92
93
    public function monthlyStatsFrom()
94
    {
95
        $statsData = $this->getMonthlyStatsData();
96
        if (!$statsData) {
97
            return null;
98
        }
99
        array_shift($statsData);
100
        $oldest = end($statsData);
101
102
        return $oldest->created_at;
103
    }
104
105
    public function getDailyStatsData($reverse = true)
106
    {
107
        $this->initDailyStatsData();
108
        if (count($this->dailyStats) < 2) {
109
            return null;
110
        }
111
        if ($reverse) {
112
            return array_reverse($this->dailyStats);
113
        }
114
115
        return $this->dailyStats;
116
    }
117
118
    public function getMonthlyStatsData($reverse = true)
119
    {
120
        $this->initMonthlyStatsData();
121
        if (count($this->monthlyStats) < 2) {
122
            return null;
123
        }
124
        if ($reverse) {
125
            return array_reverse($this->monthlyStats);
126
        }
127
128
        return $this->monthlyStats;
129
    }
130
131
    private function initDailyStatsData()
132
    {
133
        if (!$this->dailyStats) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->dailyStats of type app\models\AccountStats[] 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...
134
            $rows = $this->account->getAccountStats()
135
                ->select([
136
                    new Expression('DATE_FORMAT(created_at, "%Y-%m-%d") as day'),
137
                    'account_stats.*',
138
                ])
139
                ->andWhere(new Expression('account_stats.created_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)'))
140
                ->orderBy('account_stats.id ASC')
141
                ->all();
142
143
            foreach ($rows as $row) {
144
                $this->dailyStats[$row->day] = $row;
145
            }
146
            $this->dailyStats = array_values($this->dailyStats);
147
        }
148
    }
149
150
    private function initMonthlyStatsData()
151
    {
152
        if (!$this->monthlyStats) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->monthlyStats of type app\models\AccountStats[] 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...
153
            $rows = $this->account->getAccountStats()
154
                ->select([
155
                    new Expression('DATE_FORMAT(created_at, "%Y-%m") as month'),
156
                    'account_stats.*',
157
                ])
158
                ->andWhere(new Expression('account_stats.created_at >= DATE_SUB(NOW(), INTERVAL 1 YEAR)'))
159
                ->orderBy('account_stats.id ASC')
160
                ->all();
161
162
            foreach ($rows as $row) {
163
                $this->monthlyStats[$row->month] = $row;
164
            }
165
            $this->monthlyStats = array_values($this->monthlyStats);
166
        }
167
    }
168
}