PrefixController::actions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.0472
c 0
b 0
f 0
cc 1
nc 1
nop 0

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\hosting\controllers;
4
5
use hipanel\actions\IndexAction;
6
use hipanel\actions\SmartCreateAction;
7
use hipanel\actions\SmartDeleteAction;
8
use hipanel\actions\SmartUpdateAction;
9
use hipanel\actions\ValidateFormAction;
10
use hipanel\actions\ViewAction;
11
use hipanel\base\CrudController;
12
use hipanel\filters\EasyAccessControl;
13
use hipanel\modules\hosting\models\PrefixSearch;
14
use yii\helpers\ArrayHelper;
15
use Yii;
16
17
class PrefixController extends CrudController
18
{
19 View Code Duplication
    public function behaviors(): array
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...
20
    {
21
        return ArrayHelper::merge(parent::behaviors(), [
22
            [
23
                'class' => EasyAccessControl::class,
24
                'actions' => [
25
                    'create' => 'admin',
26
                    'update' => 'admin',
27
                    'delete' => 'admin',
28
                    '*' => 'ip.read',
29
                ],
30
            ],
31
        ]);
32
    }
33
34
    public function actions()
35
    {
36
        return array_merge(parent::actions(), [
37
            'index' => [
38
                'class' => IndexAction::class,
39
            ],
40
            'view' => [
41
                'class' => ViewAction::class,
42
                'data' => static function ($action) {
43
                    $childPrefixSearch = new PrefixSearch();
44
                    $parentPrefixSearch = clone $childPrefixSearch;
45
                    $childDataProvider = $childPrefixSearch->search([
46
                        $childPrefixSearch->formName() => [
47
                            'ip_cnts' => $action->getCollection()->first->ip,
48
                        ],
49
                    ]);
50
                    $parentDataProvider = $parentPrefixSearch->search([
51
                        $parentPrefixSearch->formName() => [
52
                            'ip_cntd' => $action->getCollection()->first->ip,
53
                        ],
54
                    ]);
55
56
                    return [
57
                        'childPrefixesDataProvider' => $childDataProvider,
58
                        'parentPrefixesDataProvider' => $parentDataProvider,
59
                    ];
60
                },
61
            ],
62
            'create' => [
63
                'class' => SmartCreateAction::class,
64
                'success' => Yii::t('hipanel.hosting.ipam', 'Prefix was created successfully'),
65
                'error' => Yii::t('hipanel.hosting.ipam', 'An error occurred when trying to add a prefix'),
66
            ],
67
            'update' => [
68
                'class' => SmartUpdateAction::class,
69
                'success' => Yii::t('hipanel.hosting.ipam', 'Prefix was updated successfully'),
70
                'error' => Yii::t('hipanel.hosting.ipam', 'An error occurred when trying to update a prefix'),
71
            ],
72
            'delete' => [
73
                'class' => SmartDeleteAction::class,
74
                'success' => Yii::t('hipanel.hosting.ipam', 'Prefix was deleted successfully'),
75
            ],
76
            'validate-form' => [
77
                'class' => ValidateFormAction::class,
78
            ],
79
            'set-note' => [
80
                'class' => SmartUpdateAction::class,
81
                'success' => Yii::t('hipanel.hosting.ipam', 'Description was changed'),
82
                'error' => Yii::t('hipanel.hosting.ipam', 'Failed to change description'),
83
            ],
84
        ]);
85
    }
86
}
87