Completed
Push — master ( 9f3049...8e4a64 )
by Paweł
03:06
created

AccountSearch::search()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 52
rs 9.0472
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
 * 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\components\AccountManager;
12
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...
13
use yii\base\Model;
14
use yii\data\ActiveDataFilter;
15
use yii\data\ActiveDataProvider;
16
use yii\helpers\ArrayHelper;
17
18
class AccountSearch extends Model
19
{
20
    public function search(array $params)
21
    {
22
        $query = Account::find()
23
            ->leftJoin(
24
                AccountStats::tableName(),
25
                'account.id=account_stats.account_id and account_stats.id = (SELECT MAX(id) FROM account_stats WHERE account_stats.account_id=account.id)'
26
            );
27
28
        $tags = ArrayHelper::getValue($params, 'filter.tags');
29
        unset($params['filter']['tags']);
30
31
        if ($tags) {
32
            $manager = \Yii::createObject(AccountManager::class);
33
            $accountIds = $manager->findByTags($tags, \Yii::$app->user->id);
34
35
            $query->andWhere(['account.id' => $accountIds]);
36
        }
37
38
        $dataFilter = \Yii::createObject([
39
            'class' => ActiveDataFilter::class,
40
            'searchModel' => DataFilterForm::class,
41
        ]);
42
        if ($dataFilter->load($params)) {
43
            $filter = $dataFilter->build();
44
            if ($filter === false) {
45
                return $dataFilter;
46
            }
47
            $query->andWhere($filter);
48
        }
49
50
        $dataProvider = new ActiveDataProvider([
51
            'query' => $query,
52
        ]);
53
54
        $dataProvider->sort->attributes['followed_by'] = [
55
            'asc' => ['account_stats.followed_by' => SORT_ASC],
56
            'desc' => ['account_stats.followed_by' => SORT_DESC],
57
        ];
58
        $dataProvider->sort->attributes['follows'] = [
59
            'asc' => ['account_stats.follows' => SORT_ASC],
60
            'desc' => ['account_stats.follows' => SORT_DESC],
61
        ];
62
        $dataProvider->sort->attributes['media'] = [
63
            'asc' => ['account_stats.media' => SORT_ASC],
64
            'desc' => ['account_stats.media' => SORT_DESC],
65
        ];
66
        $dataProvider->sort->attributes['er'] = [
67
            'asc' => ['account_stats.er' => SORT_ASC],
68
            'desc' => ['account_stats.er' => SORT_DESC],
69
        ];
70
71
        return $dataProvider;
72
    }
73
}