Passed
Push — master ( f44d0e...6ee8a6 )
by Aleksandr
02:10
created

CreateAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 19
c 2
b 0
f 0
dl 0
loc 31
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 23 7
1
<?php
2
3
4
namespace carono\yii2crud\actions;
5
6
7
use carono\yii2crud\CrudController;
8
use Yii;
9
use yii\db\ActiveRecord;
10
use yii\helpers\Html;
11
12
/**
13
 * Class CreateAction
14
 *
15
 * @package carono\yii2crud\actions
16
 * @property CrudController $controller
17
 * @method getMessageOnCreate(\yii\db\ActiveRecord $model)
18
 */
19
class CreateAction extends Action
20
{
21
    public $view = 'create';
22
    public $loadDefaultValues = true;
23
    public $loadGetParams = true;
24
    public $skipIfSet = true;
25
    public $messageOnCreate = 'Model Successful Created';
26
27
    public function run()
28
    {
29
        /**
30
         * @var ActiveRecord $class
31
         * @var ActiveRecord $model
32
         */
33
        $class = $this->modelClass ?: ($this->controller->createClass ?: $this->controller->modelClass);
34
        $model = new $class();
35
        $this->controller->beforeCreate($model);
36
        if ($this->loadDefaultValues) {
37
            $model->loadDefaultValues($this->skipIfSet);
38
        }
39
        if ($this->loadGetParams) {
40
            $model->load(\Yii::$app->request->get());
41
        }
42
        if ($model->load(Yii::$app->request->post())) {
43
            if ($model->save()) {
44
                Yii::$app->session->setFlash('success', $this->getMessageOnCreate($model));
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

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

44
                Yii::$app->session->/** @scrutinizer ignore-call */ 
45
                                    setFlash('success', $this->getMessageOnCreate($model));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
                return $this->controller->redirect($this->controller->createRedirect($model));
46
            }
47
            Yii::$app->session->setFlash('error', Html::errorSummary($model));
48
        }
49
        return $this->controller->render($this->view, ['model' => $model]);
50
    }
51
}