Completed
Push — master ( 7e94e9...69e2a4 )
by Oleg
03:23
created

Create   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 13
c 4
b 0
f 1
lcom 1
cbo 5
dl 21
loc 77
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C newModel() 0 22 9
A run() 21 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class Create extends BaseAction
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
    public function newModel()
51
    {
52
        $newModel = $this->newModel;
53
54
        if (!is_string($newModel) && !is_array($newModel) && !is_callable($newModel)) {
55
            throw new InvalidConfigException('"' . get_class($this) .
56
                '::newModel" must be set callable or the name of existing class.');
57
        }
58
59
        if (is_string($newModel) && !class_exists($newModel)) {
60
            throw new InvalidConfigException('"' . get_class($this) .
61
                '::newModel" must be set the class name of existing class.');
62
        }
63
64
        if (is_array($newModel) && isset($newModel['class']) && !class_exists($newModel['class'])) {
65
            throw new InvalidConfigException('"' . get_class($this) .
66
                '::newModel" must be an array containing a "class" element and ' .
67
                'set the class name of existing class.');
68
        }
69
70
        return \Yii::createObject($newModel);
71
    }
72
73
    /**
74
     * Creates new record.
75
     * @return mixed response
76
     */
77 View Code Duplication
    public function run()
0 ignored issues
show
Duplication introduced by
This method 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...
78
    {
79
        $model = $this->newModel();
80
        $model->setScenario($this->scenario);
81
        if ($model->load(\Yii::$app->getRequest()->post())) {
82
            if (\Yii::$app->getRequest()->isAjax) {
83
                \Yii::$app->getResponse()->format = Response::FORMAT_JSON;
84
                return ActiveForm::validate($model);
85
            }
86
            if ($model->save()) {
87
                $this->addSuccessFlash();
88
                return $this->redirect($model);
89
            } else {
90
                $this->addErrorFlash();
91
            }
92
        }
93
94
        return $this->controller->render($this->view, [
95
            $this->nameVariableModel => $model,
96
        ]);
97
    }
98
}
99