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

AccountDailyDiff   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initLastDiff() 0 8 1
A initDiff() 0 17 2
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\components\stats\base\AccountDiff;
12
use Carbon\Carbon;
13
use yii\db\Expression;
14
use yii\helpers\ArrayHelper;
15
16
/**
17
 * @inheritdoc
18
 *
19
 * @property array|\app\models\Account[] $models
20
 */
21
class AccountDailyDiff extends AccountDiff
22
{
23
    public function initLastDiff()
24
    {
25
        $accountIds = $this->getModelIds();
26
        $max1Ids = $this->getLastStatsIds(null, null, $accountIds);
27
        $max2Ids = $this->getLastStatsBeforeIds(null, null, $accountIds, 'account_stats.account_id', 1, $max1Ids);
28
29
        $accountsStats = $this->getAccountsStats($accountIds, ArrayHelper::merge($max1Ids, $max2Ids));
30
        $this->lastDiffCache = $this->prepareCache($accountsStats, 'account_id');
31
    }
32
33
    /**
34
     * The difference between the data from subsequent days from a given date range.
35
     *
36
     * @param $olderDate
37
     * @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...
38
     */
39
    public function initDiff($olderDate, $newerDate = null)
40
    {
41
        $olderDate = (new Carbon($olderDate))->subDay()->startOfDay()->toDateTimeString();
42
        $newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString();
43
44
        if (empty($this->diffCache)) {
45
            $accountIds = $this->getModelIds();
46
47
            //group by DATE Y-m-d
48
            $ids = $this->getLastStatsIds($olderDate, $newerDate, $accountIds, [
49
                'account_id',
50
                new Expression('DATE(created_at)'),
51
            ]);
52
53
            $accountsStats = $this->getAccountsStats($accountIds, $ids);
54
55
            $this->diffCache = $this->prepareCache($accountsStats, 'account_id');
56
        }
57
    }
58
}