PriceController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 134
Duplicated Lines 11.19 %

Coupling/Cohesion

Components 0
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 12
dl 15
loc 134
ccs 0
cts 107
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B actions() 0 81 2
A actionSuggest() 0 33 2
A behaviors() 15 15 1

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
 * 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\RedirectAction;
15
use hipanel\actions\SmartCreateAction;
16
use hipanel\actions\SmartDeleteAction;
17
use hipanel\actions\SmartUpdateAction;
18
use hipanel\actions\ValidateFormAction;
19
use hipanel\actions\ViewAction;
20
use hipanel\base\CrudController;
21
use hipanel\filters\EasyAccessControl;
22
use hipanel\helpers\ArrayHelper;
23
use hipanel\modules\finance\actions\PriceUpdateAction;
24
use hipanel\modules\finance\collections\PricesCollection;
25
use hipanel\modules\finance\helpers\PriceSort;
26
use hipanel\modules\finance\models\Plan;
27
use hipanel\modules\finance\models\Price;
28
use hipanel\modules\finance\models\query\PriceQuery;
29
use hipanel\modules\finance\models\TargetObject;
30
use Yii;
31
use yii\base\Event;
32
33
/**
34
 * Class PriceController.
35
 *
36
 * @author Dmytro Naumenko <[email protected]>
37
 */
38
class PriceController extends CrudController
39
{
40 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...
41
    {
42
        return array_merge(parent::behaviors(), [
43
            [
44
                'class' => EasyAccessControl::class,
45
                'actions' => [
46
                    'create' => 'price.create',
47
                    'update' => 'price.update',
48
                    'delete' => 'price.delete',
49
                    'create-suggested' => 'price.update',
50
                    '*' => ['plan.read', 'price.read'],
51
                ],
52
            ],
53
        ]);
54
    }
55
56
    public function actions()
57
    {
58
        return array_merge(parent::actions(), [
59
            'index' => [
60
                'class' => IndexAction::class,
61
                'on beforePerform' => function (Event $event) {
62
                    /** @var PriceQuery $query */
63
                    $query = $event->sender->getDataProvider()->query;
64
                    $query
65
                        ->withMainObject()
66
                        ->withPlan()
67
                        ->withFormulaLines();
68
                },
69
            ],
70
            'view' => [
71
                'class' => ViewAction::class,
72
            ],
73
            'create' => [
74
                'class' => SmartCreateAction::class,
75
                'data' => function ($action, $data) {
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...
Unused Code introduced by
The parameter $data 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...
76
                    $plan = null;
77
                    if ($plan_id = Yii::$app->request->get('plan_id')) {
78
                        $plan = Plan::findOne(['id' => $plan_id]);
79
                    }
80
81
                    return compact('plan');
82
                },
83
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully created'),
84
            ],
85
            'create-suggested' => [
86
                'class' => SmartCreateAction::class,
87
                'collection' => ['class' => PricesCollection::class],
88
                'scenario' => 'create',
89
                'POST' => [
90
                    'save' => true,
91
                    'success' => [
92
                        'class' => RedirectAction::class,
93
                        'url' => function (RedirectAction $action) {
94
                            return ['@plan/view', 'id' => $action->collection->getModel()->plan_id];
95
                        },
96
                    ],
97
                ],
98
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully created'),
99
            ],
100
            'update' => [
101
                'class' => PriceUpdateAction::class,
102
                'collection' => ['class' => PricesCollection::class],
103
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully updated'),
104
                'scenario' => 'update',
105
                'on beforeSave' => function (Event $event) {
106
                    /** @var \hipanel\actions\Action $action */
107
                    $action = $event->sender;
108
                    $action->collection->load();
109
                },
110
                'on beforeFetch' => function (Event $event) {
111
                    /** @var PriceQuery $query */
112
                    $query = $event->sender->getDataProvider()->query;
113
                    $query
114
                        ->withFormulaLines()
115
                        ->withMainObject();
116
                },
117
                'data' => function ($action, $data) {
118
                    $data['models'] = PriceSort::anyPrices()->values($data['models'], true);
119
120
                    return $data;
121
                },
122
            ],
123
            'delete' => [
124
                'class' => SmartDeleteAction::class,
125
                'success' => Yii::t('hipanel.finance.price', 'Prices were successfully deleted'),
126
            ],
127
            'set-note' => [
128
                'class' => SmartUpdateAction::class,
129
                'success' => Yii::t('hipanel', 'Note changed'),
130
            ],
131
            'validate-form' => [
132
                'class' => ValidateFormAction::class,
133
                'collection' => ['class' => PricesCollection::class],
134
            ],
135
        ]);
136
    }
137
138
    public function actionSuggest($plan_id, $object_id = null, $template_plan_id = null, $type = 'default')
139
    {
140
        $plan = Plan::findOne(['id' => $plan_id]);
141
142
        $suggestions = (new Price())->batchQuery('suggest', [
143
            'plan_id' => $plan_id,
144
            'object_id' => $object_id,
145
            'template_plan_id' => $template_plan_id,
146
            'type' => $type,
147
        ]);
148
149
        $models = [];
150
        foreach ($suggestions as $suggestion) {
151
            $object = ArrayHelper::remove($suggestion, 'object');
152
153
            /** @var Price $price */
154
            $price = Price::instantiate($suggestion);
155
            $price->setScenario('create');
156
            $price->setAttributes($suggestion);
157
            $price->populateRelation('object', new TargetObject($object));
158
159
            $models[] = $price;
160
        }
161
162
        $models = PriceSort::anyPrices()->values($models, true);
163
164
        return $this->render('suggested', [
165
            'type' => $type,
166
            'model' => reset($models),
167
            'models' => $models,
168
            'plan' => $plan,
169
        ]);
170
    }
171
}
172