Create::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4.432

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 21
loc 21
ccs 7
cts 10
cp 0.7
rs 9.0534
cc 4
eloc 14
nc 4
nop 0
crap 4.432
1
<?php
2
/**
3
 * @link https://github.com/roboapp
4
 * @copyright Copyright (c) 2016 Roboapp
5
 * @license [MIT License](https://opensource.org/licenses/MIT)
6
 */
7
8
namespace roboapp\crud;
9
10
use yii\base\InvalidConfigException;
11
use yii\base\Model;
12
use yii\db\ActiveRecord;
13
use yii\web\Response;
14
use yii\widgets\ActiveForm;
15
16
/**
17
 * Action supports creation of the new model using web form.
18
 *
19
 * @author iRipVanWinkle <[email protected]>
20
 * @since 1.0
21
 */
22 View Code Duplication
class Create extends BaseAction
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    /**
25
     * @var string the scenario to be assigned to the new model
26
     */
27
    public $scenario = Model::SCENARIO_DEFAULT;
28
29
    /**
30
     * @var string name of the view
31
     */
32
    public $view = 'create';
33
34
    /**
35
     * @var string|array|callable a PHP callable that will be called to create the new model or class name
36
     * The callable should return the new model instance.
37
     */
38
    public $newModel;
39
40
    /**
41
     * @var string the name variable model in the view template
42
     */
43
    public $nameVariableModel = 'model';
44
45
    /**
46
     * Creates new model instance.
47
     * @return ActiveRecord new model instance.
48
     * @throws InvalidConfigException on invalid configuration.
49
     */
50 18
    public function newModel()
51
    {
52 18
        return \Yii::createObject($this->newModel);
53
    }
54 18
55
    /**
56
     * Creates new record.
57
     * @return mixed response
58
     */
59 18
    public function run()
60 3
    {
61 3
        $model = $this->newModel();
62
        $model->setScenario($this->scenario);
63
        if ($model->load(\Yii::$app->getRequest()->post())) {
64 15
            if (\Yii::$app->getRequest()->isAjax) {
65
                \Yii::$app->getResponse()->format = Response::FORMAT_JSON;
66
                return ActiveForm::validate($model);
67
            }
68
            if ($model->save()) {
69
                $this->addSuccessFlash();
70 15
                return $this->redirect($model);
71
            } else {
72
                $this->addErrorFlash();
73
            }
74
        }
75
76
        return $this->controller->render($this->view, [
77 18
            $this->nameVariableModel => $model,
78
        ]);
79 18
    }
80
}
81