RequisiteController::actions()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 146

Duplication

Lines 15
Ratio 10.27 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 15
loc 146
ccs 0
cts 122
cp 0
rs 7.6888
c 0
b 0
f 0
cc 5
nc 1
nop 0
crap 30

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
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\SmartPerformAction;
15
use hipanel\actions\ViewAction;
16
use hipanel\actions\ComboSearchAction;
17
use hipanel\actions\SmartUpdateAction;
18
use hipanel\actions\PrepareBulkAction;
19
use hipanel\actions\RedirectAction;
20
use hipanel\actions\ProxyAction;
21
use hipanel\filters\EasyAccessControl;
22
use hipanel\actions\ValidateFormAction;
23
use hipanel\base\CrudController;
24
use hipanel\helpers\ArrayHelper;
25
use hipanel\modules\client\models\query\ContactQuery;
26
use yii\base\Event;
27
use Yii;
28
29
class RequisiteController extends CrudController
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function behaviors()
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...
35
    {
36
        return ArrayHelper::merge(parent::behaviors(), [
37
            [
38
                'class' => EasyAccessControl::class,
39
                'actions' => [
40
                    'reserve-number' => 'requisites.update',
41
                    'create' => 'requisites.create',
42
                    'copy' => 'requisites.create',
43
                    'update' => 'requisites.update',
44
                    'set-templates' => 'requisites.update',
45
                    'set-serie' => 'requisites.update',
46
                    '*' => 'requisites.read',
47
                ],
48
            ],
49
        ]);
50
    }
51
52
    public function actions()
53
    {
54
        return array_merge(parent::actions(), [
55
            'index' => [
56
                'class' => IndexAction::class,
57
            ],
58
            'search' => [
59
                'class' => ComboSearchAction::class,
60
            ],
61
            'view' => [
62
                'class' => ViewAction::class,
63
                'findOptions' => ['with_counters' => 1],
64
                'on beforePerform' => function ($event) {
65
                    /** @var ViewAction $action */
66
                    $action = $event->sender;
67
68
                    /** @var ContactQuery $query */
69
                    $query = $action->getDataProvider()->query;
70
71
                    if (Yii::getAlias('@document', false)) {
72
                        $query->withDocuments();
73
                    }
74
                    $query->withLocalizations();
75
                },
76
            ],
77
            'reserve-number' => [
78
                'class' => SmartUpdateAction::class,
79
                'success' => Yii::t('hipanel:finance', 'Document number was reserved'),
80
                'view' => 'modal/reserveNumber',
81
                'POST html' => [
82
                    'save' => true,
83
                    'success' => [
84
                        'class' => RedirectAction::class,
85
                        'url' => function () {
86
                            $requisite = Yii::$app->request->post('Requisite');
87
88
                            return ['@requisite/view', 'id' => $requisite['id']];
89
                        },
90
                    ],
91
                ],
92
            ],
93
            'set-templates' => [
94
                'class' => SmartUpdateAction::class,
95
                'success' => Yii::t('hipanel:finance', 'Templates changed'),
96
                'POST html' => [
97
                    'save' => true,
98
                    'success' => [
99
                        'class' => RedirectAction::class,
100
                        'url' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
                            return Yii::$app->request->referrer;
102
                        },
103
                    ],
104
                ],
105
            ],
106
            'bulk-set-templates' => [
107
                'class' => SmartUpdateAction::class,
108
                'scenario' => 'set-templates',
109
                'view' => 'modal/_bulkSetTemplates',
110
                'success' => Yii::t('hipanel:finance', 'Templates changed'),
111
                'POST' => [
112
                    'save' => true,
113
                    'success' => [
114
                        'class' => RedirectAction::class,
115
                        'url' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
                            return Yii::$app->request->referrer;
117
                        },
118
                    ],
119
                ],
120
                'collectionLoader' => function ($action) {
121
                    /** @var SmartPerformAction $action */
122
                    $data = Yii::$app->request->post($action->collection->getModel()->formName());
123
                    $attributes = [];
124
                    foreach (['invoice_id', 'acceptance_id', 'contract_id', 'probation_id', 'nda_id', 'internal_invoice_id'] as $attribute) {
125
                        $attributes[$attribute] = $data[$attribute];
126
                        unset($data[$attribute]);
127
                    }
128
129
                    foreach ($data as &$item) {
130
                        $item = array_merge($item, $attributes);
131
                    }
132
133
                    $action->collection->load($data);
134
                },
135
                'on beforeFetch' => function (Event $event) {
136
                    /** @var \hipanel\actions\SearchAction $action */
137
                    $action = $event->sender;
138
                    $dataProvider = $action->getDataProvider();
139
                    $dataProvider->query
140
                        ->select(['*'])
141
                        ->addSelect(['templates'])
142
                        ->andWhere(['show_nonrequisite' => 1]);
143
                },
144
            ],
145
            'set-templates-modal' => [
146
                'class' => PrepareBulkAction::class,
147
                'view' => 'modal/_bulkSetTemplates',
148 View Code Duplication
                'on beforePerform' => function (Event $event) {
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...
149
                    /** @var \hipanel\actions\SearchAction $action */
150
                    $action = $event->sender;
151
                    $dataProvider = $action->getDataProvider();
152
                    $dataProvider->query
153
                        ->select(['*'])
154
                        ->addSelect(['templates']);
155
                },
156
            ],
157
            'set-serie' => [
158
                'class' => SmartUpdateAction::class,
159
                'success' => Yii::t('hipanel:finance', 'Serie changed'),
160
                'error' => Yii::t('hipanel:finance', 'Failed to change requisite serie'),
161
            ],
162
            'bulk-set-serie' => [
163
                'class' => SmartUpdateAction::class,
164
                'scenario' => 'set-serie',
165
                'view' => 'modal/_bulkSetSerie',
166
                'success' => Yii::t('hipanel:finance', 'Series changed'),
167
                'collectionLoader' => function ($action) {
168
                    /** @var SmartPerformAction $action */
169
                    $data = Yii::$app->request->post($action->collection->getModel()->formName());
170
                    $serie = $data['serie'];
171
                    unset($data['serie']);
172
                    foreach ($data as &$item) {
173
                        $item['serie'] = $serie;
174
                    }
175
176
                    $action->collection->load($data);
177
                },
178
                'POST pjax' => [
179
                    'save' => true,
180
                    'success' => [
181
                        'class' => ProxyAction::class,
182
                        'action' => 'index',
183
                    ],
184
                ],
185 View Code Duplication
                'on beforeFetch' => function (Event $event) {
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...
186
                    /** @var \hipanel\actions\SearchAction $action */
187
                    $action = $event->sender;
188
                    $dataProvider = $action->getDataProvider();
189
                    $dataProvider->query
190
                        ->select(['*']);
191
                },
192
            ],
193
            'validate-form' => [
194
                'class' => ValidateFormAction::class,
195
            ],
196
        ]);
197
    }
198
}
199