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

SmartCreateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 28.79 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 19
loc 66
ccs 0
cts 53
cp 0
rs 10
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 2
A getDefaultRules() 19 51 2

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
/*
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