Update::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 21
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
cc 4
eloc 14
nc 4
nop 1
crap 4
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;
11
use yii\base\Model;
12
use yii\web\Response;
13
use yii\widgets\ActiveForm;
14
15
/**
16
 * Action supports updating of the existing model using web form.
17
 *
18
 * @author iRipVanWinkle <[email protected]>
19
 * @since 1.0
20
 */
21 View Code Duplication
class Update 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...
22
{
23
    /**
24
     * @var string the scenario to be assigned to the new model
25
     */
26
    public $scenario = Model::SCENARIO_DEFAULT;
27
28
    /**
29
     * @var string name of the view
30
     */
31
    public $view = 'update';
32
33
    /**
34
     * @var string the name variable model in the view template
35
     */
36
    public $nameVariableModel = 'model';
37
38
    /**
39
     * Updates existing record.
40
     * @param mixed $id ID of the model to be updated.
41
     * @return mixed response
42
     */
43 15
    public function run($id)
44
    {
45 15
        $model = $this->getModel($id);
46 12
        $model->setScenario($this->scenario);
47 12
        if ($model->load(Yii::$app->getRequest()->post())) {
48 9
            if (Yii::$app->getRequest()->isAjax) {
49 3
                Yii::$app->getResponse()->format = Response::FORMAT_JSON;
50 3
                return ActiveForm::validate($model);
51
            }
52 6
            if ($model->save()) {
53 3
                $this->addSuccessFlash();
54 3
                return $this->redirect($model);
55
            } else {
56 3
                $this->addErrorFlash();
57
            }
58 3
        }
59
60 6
        return $this->controller->render($this->view, [
61 6
            $this->nameVariableModel => $model,
62 6
        ]);
63
    }
64
}
65