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

AccountAccountsDataProvider   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A init() 0 25 1
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 2018-10-31
6
 */
7
8
namespace app\components\stats\providers;
9
10
11
use app\components\stats\contracts\DataProviderInterface;
12
use app\components\stats\traits\FromToDateTrait;
13
use app\components\stats\traits\StatsAttributesTrait;
14
use app\components\traits\SetAccountTrait;
15
use app\models\Account;
16
use yii\data\ActiveDataProvider;
17
18
class AccountAccountsDataProvider extends ActiveDataProvider implements DataProviderInterface
19
{
20
    use FromToDateTrait, SetAccountTrait, StatsAttributesTrait;
21
22
    public function init()
23
    {
24
        parent::init();
25
        $this->throwExceptionIfFromToAreNotSet();
26
        $this->throwExceptionIfAccountIsNotSet();
27
28
        $this->query = Account::find()
29
            ->select([
30
                'account.*',
31
                'count(account.id) as occurs',
32
            ])
33
            ->innerJoin('media_account', 'account.id=media_account.account_id')
34
            ->innerJoin('media', 'media_account.media_id=media.id')
35
            ->andWhere(['media.account_id' => $this->account->id])
36
            ->andWhere(['>=', 'media_account.created_at', $this->from->startOfDay()->toDateTimeString()])
37
            ->andWhere(['<=', 'media_account.created_at', $this->to->endOfDay()->toDateTimeString()])
38
            ->groupBy('account.id');
39
40
        $this->sort->attributes['occurs'] = [
41
            'asc' => ['occurs' => SORT_ASC],
42
            'desc' => ['occurs' => SORT_DESC],
43
        ];
44
45
        $this->sort->defaultOrder = [
46
            'occurs' => SORT_DESC,
47
        ];
48
    }
49
}