Completed
Push — master ( a52bcf...c080ac )
by Dmitry
04:27
created

SmartCreateAction::getDefaultRules()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 51
Code Lines 36

Duplication

Lines 19
Ratio 37.25 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 36
c 2
b 1
f 0
nc 1
nop 0
dl 19
loc 51
ccs 0
cts 48
cp 0
crap 6
rs 9.4109

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\actions;
13
14
/**
15
 * Class SmartCreateAction.
16
 */
17
class SmartCreateAction extends SwitchAction
18
{
19
    /**
20
     * @var string View name, used for render of result on GET request
21
     */
22
    public $view;
23
24
    public function run()
25
    {
26
        $this->view = $this->view ?: $this->getScenario();
27
        return parent::run();
28
    }
29
30
    /** {@inheritdoc} */
31
    protected function getDefaultRules()
32
    {
33
        return array_merge(parent::getDefaultRules(), [
34
            'GET ajax' => [
35
                'class' => RenderAjaxAction::class,
36
                'view' => $this->view,
37
                'data' => $this->data,
38 View Code Duplication
                'params' => function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
                    $model = $action->controller->newModel(['scenario' => $action->scenario]);
40
                    return [
41
                        'model'  => $model,
42
                        'models' => [$model],
43
                    ];
44
                },
45
            ],
46
            'GET' => [
47
                'class'  => RenderAction::class,
48
                'view'   => $this->view,
49
                'data'   => $this->data,
50 View Code Duplication
                'params' => function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
                    $model = $action->controller->newModel(['scenario' => $action->scenario]);
52
                    return [
53
                        'model'  => $model,
54
                        'models' => [$model],
55
                    ];
56
                },
57
            ],
58
            'POST' => [
59
                'save'    => true,
60
                'success' => [
61
                    'class' => RedirectAction::class,
62 View Code Duplication
                    'url'   => function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
                        return $action->collection->count() > 1
64
                            ? $action->controller->getSearchUrl(['id_in' => $action->collection->ids])
65
                            : $action->controller->getActionUrl('view', ['id' => $action->model->id]);
66
                    },
67
                ],
68
                'error'   => [
69
                    'class'  => RenderAction::class,
70
                    'view'   => $this->view,
71
                    'data'   => $this->data,
72
                    'params' => function ($action) {
73
                        return [
74
                            'model'  => $action->collection->first,
75
                            'models' => $action->collection->models,
76
                        ];
77
                    },
78
                ],
79
            ],
80
        ]);
81
    }
82
}
83