Completed
Push — master ( 567ea1...6214df )
by Igor
05:17
created

NewsController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
ccs 0
cts 47
cp 0
rs 9.0303
cc 1
eloc 36
nc 1
nop 0
crap 2
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\web\Response;
7
use yii\filters\VerbFilter;
8
use yii\helpers\Url;
9
use app\helpers\Model;
10
use app\traits\ModelTrait;
11
use app\models\News;
12
use app\modules\admin\models\search\NewsSearch;
13
14
class NewsController extends \yii\web\Controller
15
{
16
    use ModelTrait;
17
18
    public function behaviors()
19
    {
20
        return [
21
            'verbs' => [
22
                'class' => VerbFilter::class,
23
                'actions' => [
24
                    'publish' => ['post'],
25
                    'unpublish' => ['post'],
26
                    'delete' => ['post'],
27
                    'operations' => ['post'],
28
                    'preview-upload' => ['post'],
29
                    'gallery-upload' => ['post'],
30
                ],
31
            ],
32
        ];
33
    }
34
35
    public function actions()
36
    {
37
        return [
38
            'operations' => [
39
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
40
                'modelClass' => 'app\models\News',
41
                'operations' => [
42
                    'delete' => [],
43
                    'set-publish' => ['status' => News::STATUS_ACTIVE],
44
                    'set-unpublish' => ['status' => News::STATUS_BLOCKED]
45
                ]
46
            ],
47
            'set-publish' => [
48
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
49
                'modelClass' => 'app\models\News',
50
                'attributes' => ['status' => News::STATUS_ACTIVE],
51
            ],
52
            'set-unpublish' => [
53
                'class' => 'app\modules\admin\controllers\common\UpdateAttributesAction',
54
                'modelClass' => 'app\models\News',
55
                'attributes' => ['status' => News::STATUS_BLOCKED],
56
            ],
57
            'delete' => [
58
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
59
                'modelClass' => 'app\models\News',
60
            ],
61
            'text-upload' => [
62
                'class' => 'rkit\filemanager\actions\UploadAction',
63
                'modelClass' => 'app\models\News',
64
                'attribute' => 'text',
65
                'inputName' => 'file',
66
                'resultFieldPath' => 'filelink',
67
            ],
68
            'preview-upload' => [
69
                'class'     => 'rkit\filemanager\actions\UploadAction',
70
                'modelClass' => 'app\models\News',
71
                'attribute' => 'preview',
72
                'inputName' => 'file',
73
            ],
74
            'gallery-upload' => [
75
                'class'     => 'rkit\filemanager\actions\UploadAction',
76
                'modelClass' => 'app\models\News',
77
                'attribute' => 'gallery',
78
                'inputName' => 'file',
79
            ],
80
        ];
81
    }
82
83
    public function actionIndex()
84
    {
85
        $newsSearch = new NewsSearch();
86
        $dataProvider = $newsSearch->search(Yii::$app->request->get());
87
        $statuses = News::getStatuses();
88
89
        return $this->render('index', [
90
            'newsSearch' => $newsSearch,
91
            'dataProvider' => $dataProvider,
92
            'statuses' => $statuses,
93
        ]);
94
    }
95
96
    public function actionEdit($id = null)
97
    {
98
        $model = new News();
99
100
        if ($id) {
101
            $model = $this->findModel($model, $id);
0 ignored issues
show
Documentation introduced by
$model is of type object<app\models\News>, but the function expects a object<app\traits\ActiveRecord>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
        }
103
104
        if (Yii::$app->request->isPost) {
105
            if ($model->load(Yii::$app->request->post()) && $model->save()) {
106
                Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully'));
107
                $urlToModel = Url::toRoute(['edit', 'id' => $model->id]);
108
                if (Yii::$app->request->isAjax) {
109
                    Yii::$app->response->format = Response::FORMAT_JSON;
110
                    return ['redirect' => $urlToModel];
111
                }
112
                return $this->redirect($urlToModel);
113
            }
114
            if (Yii::$app->request->isAjax) {
115
                Yii::$app->response->format = Response::FORMAT_JSON;
116
                return Model::collectErrors($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by new \app\models\News() on line 98 can also be of type object<app\models\News>; however, app\helpers\Model::collectErrors() does only seem to accept object<app\helpers\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
117
            }
118
        }
119
120
        return $this->render('edit', [
121
            'model' => $model,
122
        ]);
123
    }
124
}
125