Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

PriceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 127
Duplicated Lines 11.81 %

Coupling/Cohesion

Components 0
Dependencies 14

Test Coverage

Coverage 0%

Importance

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

3 Methods

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