Completed
Push — master ( d558ad...b35973 )
by Dmitry
11s
created

PriceController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 113
Duplicated Lines 9.73 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 12
dl 11
loc 113
rs 10

3 Methods

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