Completed
Push — master ( 958dbc...2d3a59 )
by Dmitry
05:33
created

PlanController::groupSalesAndPrices()   C

Complexity

Conditions 9
Paths 80

Size

Total Lines 53
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 6.8963
c 0
b 0
f 0
cc 9
eloc 32
nc 80
nop 1

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\SmartUpdateAction;
9
use hipanel\actions\ValidateFormAction;
10
use hipanel\actions\ViewAction;
11
use hipanel\base\CrudController;
12
use hipanel\modules\finance\helpers\PlanInternalsGrouper;
13
use hipanel\modules\finance\models\Plan;
14
use hiqdev\hiart\Query;
15
use Yii;
16
use yii\base\Event;
17
use yii\web\NotFoundHttpException;
18
19
class PlanController extends CrudController
20
{
21
    public function actions()
22
    {
23
        return array_merge(parent::actions(), [
24
            'create' => [
25
                'class' => SmartCreateAction::class,
26
            ],
27
            'update' => [
28
                'class' => SmartUpdateAction::class,
29
            ],
30
            'index' => [
31
                'class' => IndexAction::class,
32
            ],
33
            'view' => [
34
                'class' => ViewAction::class,
35
                'on beforePerform' => function (Event $event) {
36
                    $action = $event->sender;
37
                    $action->getDataProvider()->query
38
                        ->joinWith('sales')
39
                        ->with([
40
                            'prices' => function (Query $query) {
41
                                $query
42
                                    ->addSelect('main_object_id')
43
                                    ->joinWith('object')
44
                                    ->limit('ALL');
45
                            },
46
                        ]);
47
                },
48
                'data' => function (Action $action, array $data) {
49
                    return array_merge($data, [
50
                        'grouper' => new PlanInternalsGrouper($data['model']),
51
                    ]);
52
                },
53
            ],
54
            'set-note' => [
55
                'class' => SmartUpdateAction::class,
56
                'success' => Yii::t('hipanel', 'Note changed'),
57
            ],
58
            'validate-form' => [
59
                'class' => ValidateFormAction::class,
60
            ],
61
        ]);
62
    }
63
64
    public function actionCreatePrices($id)
65
    {
66
        $plan = Plan::findOne(['id' => $id]);
67
        if ($plan === null) {
68
            throw new NotFoundHttpException('Not found');
69
        }
70
        $this->layout = false;
71
72
        return $this->renderAjax('_createPrices', ['plan' => $plan]);
73
    }
74
}
75