Passed
Push — master ( 1a706b...988f2a )
by Paweł
02:31
created

AccountSearch::search()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 57
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 31
nc 3
nop 1
dl 0
loc 57
rs 9.424
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\modules\admin\models;
4
5
use app\components\AccountManager;
6
use app\models\AccountStats;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, app\modules\admin\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...
7
use app\models\AccountTag;
8
use app\models\Tag;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, app\modules\admin\models\Tag. 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...
9
use yii\base\Model;
10
use yii\data\ActiveDataProvider;
11
use yii\db\Expression;
12
13
/**
14
 * AccountSearch represents the model behind the search form of `app\models\Account`.
15
 */
16
class AccountSearch extends Account
17
{
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function rules()
23
    {
24
        return [
25
            [['id', 'monitoring', 'proxy_id'], 'integer'],
26
            [['username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 's_tags'], 'safe'],
27
        ];
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function scenarios()
34
    {
35
        // bypass scenarios() implementation in the parent class
36
        return Model::scenarios();
37
    }
38
39
    /**
40
     * Creates data provider instance with search query applied
41
     *
42
     * @param array $params
43
     *
44
     * @return ActiveDataProvider
45
     * @throws \yii\base\InvalidConfigException
46
     */
47
    public function search($params)
48
    {
49
        $userId = (int)\Yii::$app->user->id;
50
51
        $query = Account::find()
52
            ->select([
53
                'account.*',
54
                new Expression('GROUP_CONCAT(tag.name SEPARATOR \', \') as s_tags'),
55
            ])
56
            ->leftJoin(AccountTag::tableName(), 'account.id=account_tag.account_id AND account_tag.user_id=' . $userId)
57
            ->leftJoin(Tag::tableName(), 'account_tag.tag_id=tag.id')
58
            ->groupBy('account.id');
59
60
        // add conditions that should always apply here
61
62
        $dataProvider = new ActiveDataProvider([
63
            'query' => $query,
64
        ]);
65
66
        $dataProvider->sort->defaultOrder = [
67
            'is_valid' => SORT_ASC,
68
            'invalidation_type_id' => SORT_DESC,
69
            'invalidation_count' => SORT_DESC,
70
            'id' => SORT_DESC,
71
        ];
72
73
        $dataProvider->sort->attributes['username'] = [
74
            'asc' => ['name' => SORT_ASC, 'username' => SORT_ASC],
75
            'desc' => ['name' => SORT_DESC, 'username' => SORT_DESC],
76
        ];
77
78
        $this->load($params);
79
80
        if (!$this->validate()) {
81
            // uncomment the following line if you do not want to return any records when validation fails
82
            // $query->where('0=1');
83
            return $dataProvider;
84
        }
85
86
        // grid filtering conditions
87
        $query->andFilterWhere([
88
            'monitoring' => $this->monitoring,
89
        ]);
90
91
        $query->andFilterWhere(['or',
92
            ['like', 'username', $this->username],
93
            ['like', 'account.name', $this->username],
94
        ]);
95
96
        if ($this->s_tags) {
97
            $manager = \Yii::createObject(AccountManager::class);
98
            $accountIds = $manager->findByTags($this->s_tags, $userId);
99
100
            $query->andWhere(['account.id' => $accountIds]);
101
        }
102
103
        return $dataProvider;
104
    }
105
}
106