Completed
Push — master ( 04057c...846ed7 )
by Dmitry
05:05
created

PlanController::actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 0
cts 61
cp 0
rs 9.4743
c 0
b 0
f 0
cc 1
eloc 44
nc 1
nop 0
crap 2

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
namespace hipanel\modules\finance\controllers;
4
5
use hipanel\actions\Action;
6
use hipanel\actions\IndexAction;
7
use hipanel\actions\SmartCreateAction;
8
use hipanel\actions\SmartDeleteAction;
9
use hipanel\actions\SmartPerformAction;
10
use hipanel\actions\SmartUpdateAction;
11
use hipanel\actions\ValidateFormAction;
12
use hipanel\actions\ViewAction;
13
use hipanel\base\CrudController;
14
use hipanel\modules\finance\helpers\PlanInternalsGrouper;
15
use hipanel\modules\finance\models\Plan;
16
use hipanel\filters\EasyAccessControl;
17
use hiqdev\hiart\Query;
18
use Yii;
19
use yii\base\Event;
20
use yii\web\NotFoundHttpException;
21
use yii\web\Response;
22
23
class PlanController extends CrudController
24
{
25 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...
26
    {
27
        return array_merge(parent::behaviors(), [
28
            [
29
                'class' => EasyAccessControl::class,
30
                'actions' => [
31
                    'create' => 'plan.create',
32
                    'update' => 'plan.update',
33
                    'templates' => 'plan.create',
34
                    '*' => 'plan.read',
35
                ],
36
            ],
37
        ]);
38
    }
39
40
    public function actions()
41
    {
42
        return array_merge(parent::actions(), [
43
            'create' => [
44
                'class' => SmartCreateAction::class,
45
                'success' => Yii::t('hipanel.finance.plan', 'Plan was successfully created')
46
            ],
47
            'update' => [
48
                'class' => SmartUpdateAction::class,
49
                'success' => Yii::t('hipanel.finance.plan', 'Plan was successfully updated')
50
            ],
51
            'index' => [
52
                'class' => IndexAction::class,
53
            ],
54
            'view' => [
55
                'class' => ViewAction::class,
56
                'on beforePerform' => function (Event $event) {
57
                    $action = $event->sender;
58
                    $action->getDataProvider()->query
59
                        ->joinWith('sales')
60
                        ->andWhere(['state' => ['ok', 'deleted']])
61
                        ->with([
62
                            'prices' => function (Query $query) {
63
                                $query
64
                                    ->addSelect('main_object_id')
65
                                    ->joinWith('object')
66
                                    ->limit('ALL');
67
                            },
68
                        ]);
69
                },
70
                'data' => function (Action $action, array $data) {
71
                    return array_merge($data, [
72
                        'grouper' => new PlanInternalsGrouper($data['model']),
73
                    ]);
74
                },
75
            ],
76
            'set-note' => [
77
                'class' => SmartUpdateAction::class,
78
                'success' => Yii::t('hipanel', 'Note changed'),
79
            ],
80
            'validate-form' => [
81
                'class' => ValidateFormAction::class,
82
            ],
83
            'validate-single-form' => [
84
                'class' => ValidateFormAction::class,
85
                'validatedInputId' => false,
86
            ],
87
            'delete' => [
88
                'class' => SmartDeleteAction::class,
89
                'success' => Yii::t('hipanel.finance.plan', 'Plan was successfully deleted')
90
            ],
91
            'restore' => [
92
                'class' => SmartPerformAction::class,
93
                'success' => Yii::t('hipanel.finance.plan', 'Plan was successfully restored')
94
            ],
95
            'copy' => [
96
                'class' => SmartUpdateAction::class,
97
                'view' => 'modals/copy',
98
                'queryOptions' => ['batch' => false],
99
            ],
100
        ]);
101
    }
102
103
    public function actionCreatePrices($id)
104
    {
105
        $plan = Plan::findOne(['id' => $id]);
106
        if ($plan === null) {
107
            throw new NotFoundHttpException('Not found');
108
        }
109
        $this->layout = false;
110
111
        return $this->renderAjax('_createPrices', ['plan' => $plan]);
112
    }
113
114
    /**
115
     * @param string $object_id
116
     * @param string $plan_id
117
     */
118
    public function actionTemplates($plan_id, $object_id)
119
    {
120
        $templates = (new Plan())->query('search-templates', [
121
            'id' => $plan_id,
122
            'object_id' => $object_id,
123
        ]);
124
125
        Yii::$app->response->format = Response::FORMAT_JSON;
126
127
        return $templates;
128
    }
129
}
130