Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

InstancesController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 116
ccs 0
cts 84
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 35 1
A verbs() 0 8 1
A actions() 0 9 1
A actionUpdate() 0 18 2
A actionDelete() 0 18 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\providers;
4
5
use Craft;
6
use craft\helpers\ArrayHelper;
7
use flipbox\patron\actions\provider\instance\Create;
8
use flipbox\patron\actions\provider\instance\Delete;
9
use flipbox\patron\actions\provider\instance\Update;
10
use flipbox\patron\cp\controllers\AbstractController;
11
12
class InstancesController extends AbstractController
13
{
14
    /**
15
     * @return array
16
     */
17
    public function behaviors()
18
    {
19
        return ArrayHelper::merge(
20
            parent::behaviors(),
21
            [
22
                'error' => [
23
                    'default' => 'instance'
24
                ],
25
                'redirect' => [
26
                    'only' => ['create', 'update', 'delete'],
27
                    'actions' => [
28
                        'create' => [201],
29
                        'update' => [200],
30
                        'delete' => [204],
31
                    ]
32
                ],
33
                'flash' => [
34
                    'actions' => [
35
                        'create' => [
36
                            201 => Craft::t('patron', "Provider instance successfully created."),
37
                            400 => Craft::t('patron', "Failed to create provider instance.")
38
                        ],
39
                        'update' => [
40
                            200 => Craft::t('patron', "Provider instance successfully updated."),
41
                            400 => Craft::t('patron', "Failed to update provider instance.")
42
                        ],
43
                        'delete' => [
44
                            204 => Craft::t('patron', "Provider instance successfully deleted."),
45
                            400 => Craft::t('patron', "Failed to delete provider instance.")
46
                        ]
47
                    ]
48
                ]
49
            ]
50
        );
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    protected function verbs(): array
57
    {
58
        return [
59
            'create' => ['post'],
60
            'update' => ['post', 'put'],
61
            'delete' => ['post', 'delete']
62
        ];
63
    }
64
65
    /**
66
     * @return array
67
     */
68
    public function actions()
69
    {
70
        return [
71
            'create' => [
72
                'class' => Create::class,
73
                'checkAccess' => [$this, 'checkAdminAccess']
74
            ]
75
        ];
76
    }
77
78
    /**
79
     * @param string|int|null $instance
80
     * @return mixed
81
     * @throws \yii\base\InvalidConfigException
82
     * @throws \yii\web\BadRequestHttpException
83
     */
84
    public function actionUpdate($instance = null)
85
    {
86
        if ($instance === null) {
87
            $instance = Craft::$app->getRequest()->getRequiredBodyParam('instance');
88
        }
89
90
        $action = Craft::createObject([
91
            'class' => Update::class,
92
            'checkAccess' => [$this, 'checkAdminAccess']
93
        ], [
94
            'update',
95
            $this
96
        ]);
97
98
        return $action->runWithParams([
99
            'instance' => $instance
100
        ]);
101
    }
102
103
    /**
104
     * @param string|int|null $instance
105
     * @return mixed
106
     * @throws \yii\base\InvalidConfigException
107
     * @throws \yii\web\BadRequestHttpException
108
     */
109
    public function actionDelete($instance = null)
110
    {
111
        if ($instance === null) {
112
            $instance = Craft::$app->getRequest()->getRequiredBodyParam('instance');
113
        }
114
115
        $action = Craft::createObject([
116
            'class' => Delete::class,
117
            'checkAccess' => [$this, 'checkAdminAccess']
118
        ], [
119
            'delete',
120
            $this
121
        ]);
122
123
        return $action->runWithParams([
124
            'instance' => $instance
125
        ]);
126
    }
127
}
128