Passed
Push — master ( 7ba57e...634515 )
by Paweł
03:13
created

AccountDiffDataProvider::prepareModels()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 12
nop 0
dl 0
loc 33
rs 8.9297
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2019-01-24
6
 */
7
8
namespace app\components\stats\providers;
9
10
11
use app\components\stats\contracts\DataProviderInterface;
12
use app\dictionaries\Grouping;
13
use yii\helpers\ArrayHelper;
14
15
class AccountDiffDataProvider extends AccountDataProvider implements DataProviderInterface
16
{
17
    protected function prepareModels()
18
    {
19
        $models = [];
20
21
        $dbFrom = $this->from->copy();
22
        switch ($this->grouping) {
23
            case Grouping::MONTHLY:
24
                $dbFrom = $dbFrom->subMonth()->endOfMonth();
25
                break;
26
            case Grouping::WEEKLY:
27
                $dbFrom = $dbFrom->subWeek()->endOfWeek();
28
                break;
29
            case Grouping::DAILY:
30
            default:
31
                $dbFrom = $dbFrom->subDay()->endOfDay();
32
        }
33
        $dbFrom = $this->findDbDate($dbFrom);
34
35
        $dbTo = $this->to->copy()->endOfDay()->toDateTimeString();
36
37
        $idsQuery = $this->findStatsIds($dbFrom, $dbTo, false);
0 ignored issues
show
Bug introduced by
It seems like $dbFrom can also be of type false; however, parameter $dbFrom of app\components\stats\pro...rovider::findStatsIds() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $idsQuery = $this->findStatsIds(/** @scrutinizer ignore-type */ $dbFrom, $dbTo, false);
Loading history...
38
        $data = $this->findDataModels($idsQuery);
39
40
        $older = array_shift($data);
41
        foreach ($data as $stats) {
42
            foreach ($this->statsAttributes as $statsAttribute) {
43
                $value = ArrayHelper::getValue($stats, $statsAttribute, 0) - ArrayHelper::getValue($older, $statsAttribute, 0);
44
                $models[$stats['created_at']][$statsAttribute] = $value;
45
            }
46
            $older = $stats;
47
        }
48
49
        return $models;
50
    }
51
}