Passed
Push — master ( 62400b...78a114 )
by Aleksandr
03:25
created

CrudController::getModelQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\yii2crud;
5
6
7
use carono\yii2crud\actions\CreateAction;
8
use carono\yii2crud\actions\DeleteAction;
9
use carono\yii2crud\actions\DeleteBatch;
10
use carono\yii2crud\actions\IndexAction;
11
use carono\yii2crud\actions\UpdateAction;
12
use carono\yii2helpers\QueryHelper;
13
use Yii;
14
use yii\data\ActiveDataProvider;
15
use yii\data\BaseDataProvider;
16
use yii\db\ActiveQuery;
17
use yii\db\ActiveRecord;
18
use yii\filters\VerbFilter;
19
use yii\web\Controller;
20
use yii\web\NotFoundHttpException;
21
22
abstract class CrudController extends Controller
23
{
24
    public const SCENARIO_CREATE = 'create';
25
26
    /**
27
     * @var ActiveRecord
28
     */
29
    public $modelClass;
30
    /**
31
     * @var ActiveRecord
32
     */
33
    public $modelSearchClass;
34
35
    public $createClass;
36
37
    public $updateClass;
38
39
    public $updateView = 'update';
40
    public $indexView = 'index';
41
    public $viewView = 'view';
42
    public $createView = 'create';
43
44
    /**
45
     * @param ActiveRecord|string $class
46
     * @return ActiveQuery
47
     */
48
    public function getModelQuery($class)
49
    {
50
        return $class::find();
51
    }
52
53
    /**
54
     * @param $id
55
     * @param null $class
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $class is correct as it would always require null to be passed?
Loading history...
56
     * @return ActiveRecord
57
     * @throws NotFoundHttpException
58
     */
59
    public function findModel($id, $class = null): ActiveRecord
60
    {
61
        /**
62
         * @var ActiveRecord $class
63
         */
64
        $class = $class ?? $this->modelClass;
65
        $query = $this->getModelQuery($class)->andWhere(['id' => (int)$id]);
66
        $this->findModelCondition($query);
67
        if (($model = $query->one()) !== null) {
68
            return $model;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $model could return the type array which is incompatible with the type-hinted return yii\db\ActiveRecord. Consider adding an additional type-check to rule them out.
Loading history...
69
        }
70
        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
71
    }
72
73
    /**
74
     * @param ActiveQuery $query
75
     * @return ActiveDataProvider
76
     */
77
    public function queryToDataProvider($query): ActiveDataProvider
78
    {
79
        /**
80
         * @var ActiveRecord $modelClass
81
         */
82
        $modelClass = $query->modelClass;
83
        $table = $modelClass::tableName();
84
        $options = [
85
            'sort' => [
86
                'defaultOrder' => [
87
                    current(Yii::$app->db->getTableSchema($table)->primaryKey) => SORT_ASC
88
                ]
89
            ],
90
        ];
91
        return new ActiveDataProvider(array_merge(['query' => $query], $options));
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function behaviors()
98
    {
99
        return array_merge(parent::behaviors(), [
100
            'verbs' => [
101
                'class' => VerbFilter::class,
102
                'actions' => [
103
                    'delete' => ['POST'],
104
                    'delete-batch' => ['POST']
105
                ],
106
            ],
107
        ]);
108
    }
109
110
    public function applySearch(ActiveQuery $query, BaseDataProvider $dataProvider, $searchModel): void
0 ignored issues
show
Unused Code introduced by
The parameter $dataProvider is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

110
    public function applySearch(ActiveQuery $query, /** @scrutinizer ignore-unused */ BaseDataProvider $dataProvider, $searchModel): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        QueryHelper::regular($searchModel, $query);
113
    }
114
115
    /**
116
     * @param ActiveQuery $query
117
     * @return ActiveQuery
118
     */
119
    public function findModelCondition($query): ActiveQuery
120
    {
121
        return $query;
122
    }
123
124
    /**
125
     * @param ActiveQuery $query
126
     * @return ActiveQuery
127
     */
128
    public function indexCondition($query): ActiveQuery
129
    {
130
        return $query;
131
    }
132
133
    /**
134
     * @param $params
135
     * @return array
136
     */
137
    public function indexParams($params): array
138
    {
139
        return $params;
140
    }
141
142
    /**
143
     * @param $id
144
     * @return mixed|string
145
     */
146
    public function actionView($id)
147
    {
148
        if (Yii::$app->request->isPost) {
149
            return $this->runAction('update', ['id' => $this->id]);
150
        }
151
        return $this->render('view', [
152
            'model' => $this->findModel($id),
153
        ]);
154
    }
155
156
    /**
157
     * @param $model
158
     */
159
    public function beforeCreate($model): void
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

159
    public function beforeCreate(/** @scrutinizer ignore-unused */ $model): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
160
    {
161
    }
162
163
    /**
164
     * @param $model
165
     * @return array
166
     */
167
    public function createRedirect($model): array
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

167
    public function createRedirect(/** @scrutinizer ignore-unused */ $model): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
    {
169
        return ['index'];
170
    }
171
172
    /**
173
     * @param $model
174
     * @return array
175
     */
176
    public function updateRedirect($model): array
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
    public function updateRedirect(/** @scrutinizer ignore-unused */ $model): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
    {
178
        return ['index'];
179
    }
180
181
    /**
182
     * @param $model
183
     * @return array
184
     */
185
    public function deleteRedirect($model): array
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

185
    public function deleteRedirect(/** @scrutinizer ignore-unused */ $model): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
186
    {
187
        return ['index'];
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function actions()
194
    {
195
        return [
196
            'update' => [
197
                'class' => UpdateAction::class,
198
            ],
199
            'index' => [
200
                'class' => IndexAction::class
201
            ],
202
            'create' => [
203
                'class' => CreateAction::class
204
            ],
205
            'delete' => [
206
                'class' => DeleteAction::class
207
            ],
208
            'delete-batch' => [
209
                'class' => DeleteBatch::class
210
            ]
211
        ];
212
    }
213
}