Completed
Push — master ( e4197d...f733ed )
by Igor
11:05 queued 09:33
created

NewsSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 52
ccs 10
cts 15
cp 0.6667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 10 1
B search() 0 29 3
1
<?php
2
3
namespace app\modules\admin\models\search;
4
5
use Yii;
6
use yii\base\Model;
7
use yii\data\ActiveDataProvider;
8
use app\models\News;
9
10
/**
11
 * NewsSearch represents the model behind the search form about `app\models\News`.
12
 */
13
class NewsSearch extends News
14
{
15
    /**
16
     * @inheritdoc
17
     */
18 4
    public function rules()
19
    {
20
        return [
21 4
            ['title', 'string'],
22
            ['date_pub', 'date', 'format' => 'yyyy-mm-dd'],
23
24
            ['status', 'integer'],
25 4
            ['status', 'in', 'range' => array_keys(News::getStatuses())],
26
        ];
27
    }
28
29
    /**
30
     * Search by request criteria.
31
     *
32
     * @param array|null Filter params.
33
     * @return ActiveDataProvider Data provider.
34
     */
35 4
    public function search($params)
36
    {
37 4
        $query = News::find();
38
39 4
        $dataProvider = new ActiveDataProvider([
40 4
            'query' => $query,
41
            'sort' => [
42
                'defaultOrder' => [
43 4
                    'date_pub' => SORT_DESC,
44
                ]
45
            ],
46
            'pagination' => [
47
                'pageSize' => 50,
48
            ],
49
        ]);
50
51 4
        if (!($this->load($params) && $this->validate())) {
52 4
            return $dataProvider;
53
        }
54
55
        $query->andFilterWhere([
56
            'status' => $this->status,
57
            'DATE(date_pub)' => $this->date_pub
58
        ]);
59
60
        $query->andFilterWhere(['like', 'news.title', $this->title]);
61
62
        return $dataProvider;
63
    }
64
}
65