Passed
Push — master ( 6b49ff...b93f48 )
by Paweł
05:24
created

AccountDaily::initDiff()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 2
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.06.2018
6
 */
7
8
namespace app\components\stats;
9
10
11
use app\models\Account;
12
use app\models\AccountStats;
13
use Carbon\Carbon;
14
use yii\base\Component;
15
use yii\db\Expression;
16
use yii\helpers\ArrayHelper;
17
18
class AccountDaily extends Component
19
{
20
    /**
21
     * @var \app\models\Account
22
     */
23
    public $model;
24
25
    private $statsAttributes = [
26
        'media',
27
        'followed_by',
28
        'follows',
29
        'er',
30
    ];
31
32
    private static $cache = [];
33
34
    public function __construct(Account $model, array $config = [])
35
    {
36
        $this->model = $model;
37
        static::$cache = [];
0 ignored issues
show
Bug introduced by
Since $cache is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $cache to at least protected.
Loading history...
38
        parent::__construct($config);
39
    }
40
41
    public function get()
42
    {
43
        return static::$cache;
0 ignored issues
show
Bug introduced by
Since $cache is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $cache to at least protected.
Loading history...
44
    }
45
46
    /**
47
     * The difference between the data from subsequent days from a given date range.
48
     *
49
     * @param $olderDate
50
     * @param null $newerDate
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $newerDate is correct as it would always require null to be passed?
Loading history...
51
     * @return \app\components\stats\AccountDaily
52
     */
53
    public function initDiff($olderDate, $newerDate = null)
54
    {
55
        $olderDate = (new Carbon($olderDate))->startOfDay()->toDateTimeString();
56
        $newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString();
57
58
        if (empty(static::$cache)) {
0 ignored issues
show
Bug introduced by
Since $cache is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $cache to at least protected.
Loading history...
59
            //group by DATE Y-m-d
60
            $ids = AccountStats::find()
61
                ->select(new Expression('MAX(id) as id'))
62
                ->andWhere(['account_id' => $this->model->id])
63
                ->andWhere(['>=', 'created_at', $olderDate])
64
                ->andWhere(['<=', 'created_at', $newerDate])
65
                ->groupBy(new Expression('DATE(created_at)'))
66
                ->column();
67
68
            $stats = AccountStats::find()
69
                ->select([
70
                    'account_stats.*',
71
                    new Expression('DATE(created_at) as created_at'),
72
                ])
73
                ->andWhere(['account_id' => $this->model->id])
74
                ->andWhere(['id' => $ids])
75
                ->orderBy('id ASC')
76
                ->asArray()
77
                ->all();
78
79
            foreach ($stats as $stat) {
80
                foreach ($this->statsAttributes as $statsAttribute) {
81
                    $value = ArrayHelper::getValue($stat, $statsAttribute, 0);
82
                    static::$cache[$stat['created_at']][$statsAttribute] = $value;
83
                }
84
            }
85
        }
86
87
        return $this;
88
    }
89
}