Completed
Push — master ( ad5f15...06e8c3 )
by Andrii
04:31
created

BillController::actions()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 73
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 73
rs 8.6829
c 0
b 0
f 0
cc 4
eloc 46
nc 1
nop 0

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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\controllers;
12
13
use hipanel\actions\IndexAction;
14
use hipanel\actions\OrientationAction;
15
use hipanel\actions\RedirectAction;
16
use hipanel\actions\SmartCreateAction;
17
use hipanel\actions\SmartPerformAction;
18
use hipanel\actions\SmartUpdateAction;
19
use hipanel\actions\ValidateFormAction;
20
use hipanel\actions\ViewAction;
21
use hipanel\modules\client\controllers\ContactController;
22
use hipanel\modules\finance\forms\BillImportForm;
23
use hipanel\modules\finance\models\Bill;
24
use hipanel\modules\finance\providers\BillTypesProvider;
25
use Yii;
26
use yii\base\Module;
27
use yii\filters\AccessControl;
28
29
class BillController extends \hipanel\base\CrudController
30
{
31
    /**
32
     * @var BillTypesProvider
33
     */
34
    private $billTypesProvider;
35
36
    public function __construct($id, Module $module, BillTypesProvider $billTypesProvider, array $config = [])
37
    {
38
        parent::__construct($id, $module, $config);
39
40
        $this->billTypesProvider = $billTypesProvider;
41
    }
42
43
    public function behaviors()
44
    {
45
        return array_merge(parent::behaviors(), [
46
            'access-bill' => [
47
                'class' => AccessControl::class,
48
                'only' => ['index', 'view', 'create', 'update', 'delete'],
49
                'rules' => [
50
                    [
51
                        'allow' => true,
52
                        'roles' => ['manage', 'deposit'],
53
                        'actions' => ['index', 'view'],
54
                    ],
55
                    [
56
                        'allow' => true,
57
                        'roles' => ['bill.create'],
58
                        'actions' => ['create', 'import', 'copy'],
59
                    ],
60
                    [
61
                        'allow' => true,
62
                        'roles' => ['bill.update'],
63
                        'actions' => ['update'],
64
                    ],
65
                    [
66
                        'allow' => true,
67
                        'roles' => ['bill.delete'],
68
                        'actions' => ['delete'],
69
                    ],
70
                ],
71
            ],
72
        ]);
73
    }
74
75
    public function actions()
76
    {
77
        return [
78
            'set-orientation' => [
79
                'class' => OrientationAction::class,
80
                'allowedRoutes' => [
81
                    '@bill/index',
82
                ],
83
            ],
84
            'index' => [
85
                'class' => IndexAction::class,
86
                'data' => 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...
87
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
88
89
                    return compact('billTypes', 'billGroupLabels');
90
                },
91
            ],
92
            'view' => [
93
                'class' => ViewAction::class,
94
            ],
95
            'validate-form' => [
96
                'class' => ValidateFormAction::class,
97
            ],
98
            'create' => [
99
                'class' => SmartCreateAction::class,
100
                'data' => 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
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
102
103
                    return compact('billTypes', 'billGroupLabels');
104
                },
105
                'success' => Yii::t('hipanel:finance', 'Payment was created successfully'),
106
            ],
107
            'update' => [
108
                'class' => SmartUpdateAction::class,
109
                'success' => Yii::t('hipanel:finance', 'Payment was updated successfully'),
110
                'data' => 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...
111
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
112
113
                    return compact('billTypes', 'billGroupLabels');
114
                },
115
            ],
116
            'copy' => [
117
                'class' => SmartUpdateAction::class,
118
                'scenario' => 'create',
119
                'data' => function ($action, $data) {
120
                    foreach ($data['models'] as $model) {
121
                        /** @var Bill $model */
122
                        $model->prepareToCopy();
123
                    }
124
125
                    list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
126
127
                    return compact('billTypes', 'billGroupLabels');
128
                },
129
            ],
130
            'delete' => [
131
                'class' => SmartPerformAction::class,
132
                'success' => Yii::t('hipanel:finance', 'Payment was deleted successfully'),
133
            ],
134
            'requisites' => [
135
                'class' => RedirectAction::class,
136
                '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...
137
                    $identity = Yii::$app->user->identity;
138
                    $seller = $identity->type === $identity::TYPE_RESELLER ? $identity->username : $identity->seller;
139
                    if ($seller === 'bullet') {
140
                        $seller = 'dsr';
141
                    }
142
143
                    return array_merge(ContactController::getSearchUrl(['client' => $seller]), ['representation' => 'requisites']);
144
                },
145
            ],
146
        ];
147
    }
148
149
    public function actionImport()
150
    {
151
        $model = new BillImportForm([
152
            'billTypes' => array_filter($this->getPaymentTypes(), function ($key) {
153
                // Kick out items that are categories names, but not real types
154
                return strpos($key, ',') !== false;
155
            }, ARRAY_FILTER_USE_KEY),
156
        ]);
157
158
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
159
            $models = $model->parse();
160
161
            if ($models !== false) {
162
                list($billTypes, $billGroupLabels) = $this->getTypesAndGroups();
163
164
                return $this->render('create', [
165
                    'models' => $models,
166
                    'model' => reset($models),
167
                    'billTypes' => $billTypes,
168
                    'billGroupLabels' => $billGroupLabels,
169
                ]);
170
            }
171
        }
172
173
        return $this->render('import', ['model' => $model]);
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function getPaymentTypes()
180
    {
181
        return $this->billTypesProvider->getTypesList();
182
    }
183
184
    /**
185
     * @return array
186
     */
187
    private function getTypesAndGroups()
188
    {
189
        return $this->billTypesProvider->getGroupedList();
190
    }
191
}
192