Completed
Push — master ( 0daec9...07eda8 )
by Paweł
02:54
created

AccountSearch   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 0 57 5
A getTaggedAccountIds() 0 9 1
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 14.05.2018
6
 */
7
8
namespace app\modules\api\v1\models;
9
10
11
use app\models\AccountStats;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, app\modules\api\v1\models\AccountStats. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
use app\models\AccountTag;
13
use yii\base\Model;
14
use yii\data\ActiveDataFilter;
15
use yii\data\ActiveDataProvider;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Inflector;
18
use yii\helpers\StringHelper;
19
20
class AccountSearch extends Model
21
{
22
    public function search(array $params)
23
    {
24
        $query = Account::find()
25
            ->leftJoin(
26
                AccountStats::tableName(),
27
                'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)'
28
            );
29
30
        $tags = ArrayHelper::getValue($params, 'filter.tags');
31
        unset($params['filter']['tags']);
32
33
        if ($tags) {
34
            $tags = StringHelper::explode($tags, ',', true, true);
35
            $tag = array_shift($tags);
36
            $accountIds = $this->getTaggedAccountIds($tag);
37
38
            foreach ($tags as $tag) {
39
                $accountIds = array_intersect($accountIds, $this->getTaggedAccountIds($tag));
40
            }
41
42
            $query->andWhere(['account.id' => $accountIds]);
43
        }
44
45
        $dataFilter = \Yii::createObject([
46
            'class' => ActiveDataFilter::class,
47
            'searchModel' => DataFilterForm::class,
48
        ]);
49
        if ($dataFilter->load($params)) {
50
            $filter = $dataFilter->build();
51
            if ($filter === false) {
52
                return $dataFilter;
53
            }
54
            $query->andWhere($filter);
55
        }
56
57
        $dataProvider = new ActiveDataProvider([
58
            'query' => $query,
59
        ]);
60
61
        $dataProvider->sort->attributes['followed_by'] = [
62
            'asc' => ['account_stats.followed_by' => SORT_ASC],
63
            'desc' => ['account_stats.followed_by' => SORT_DESC],
64
        ];
65
        $dataProvider->sort->attributes['follows'] = [
66
            'asc' => ['account_stats.follows' => SORT_ASC],
67
            'desc' => ['account_stats.follows' => SORT_DESC],
68
        ];
69
        $dataProvider->sort->attributes['media'] = [
70
            'asc' => ['account_stats.media' => SORT_ASC],
71
            'desc' => ['account_stats.media' => SORT_DESC],
72
        ];
73
        $dataProvider->sort->attributes['er'] = [
74
            'asc' => ['account_stats.er' => SORT_ASC],
75
            'desc' => ['account_stats.er' => SORT_DESC],
76
        ];
77
78
        return $dataProvider;
79
    }
80
81
    /**
82
     * @param $tag
83
     * @return array
84
     */
85
    private function getTaggedAccountIds($tag): array
86
    {
87
        $accountIds = AccountTag::find()
88
            ->select('account_id')
89
            ->innerJoinWith('tag')
90
            ->andFilterWhere(['tag.slug' => Inflector::slug($tag)])
91
            ->column();
92
93
        return $accountIds;
94
    }
95
}