ConnectionsController::actionUpdate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot\cp\controllers\settings;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\craft\hubspot\actions\connections\CreateConnection;
14
use flipbox\craft\hubspot\actions\connections\DeleteConnection;
15
use flipbox\craft\hubspot\actions\connections\UpdateConnection;
16
use flipbox\craft\hubspot\cp\controllers\AbstractController;
17
use flipbox\craft\hubspot\cp\Cp;
18
use flipbox\craft\hubspot\HubSpot;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @property Cp $module
25
 */
26
class ConnectionsController extends AbstractController
27
{
28
    /**
29
     * @return array
30
     */
31
    public function behaviors()
32
    {
33
        return ArrayHelper::merge(
34
            parent::behaviors(),
35
            [
36
                'error' => [
37
                    'default' => 'connection'
38
                ],
39
                'redirect' => [
40
                    'only' => ['create', 'update', 'delete'],
41
                    'actions' => [
42
                        'create' => [201],
43
                        'update' => [200],
44
                        'delete' => [204],
45
                    ]
46
                ],
47
                'flash' => [
48
                    'actions' => [
49
                        'create' => [
50
                            201 => HubSpot::t("Connection successfully created."),
51
                            400 => HubSpot::t("Failed to create connection.")
52
                        ],
53
                        'update' => [
54
                            200 => HubSpot::t("Connection successfully updated."),
55
                            400 => HubSpot::t("Failed to update connection.")
56
                        ],
57
                        'delete' => [
58
                            204 => HubSpot::t("Connection successfully deleted."),
59
                            400 => HubSpot::t("Failed to delete connection.")
60
                        ]
61
                    ]
62
                ]
63
            ]
64
        );
65
    }
66
67
    /**
68
     * @return mixed
69
     * @throws \yii\base\InvalidConfigException
70
     */
71
    public function actionCreate()
72
    {
73
        /** @var \yii\base\Action $action */
74
        $action = Craft::createObject([
75
            'class' => CreateConnection::class
76
        ], [
77
            'update',
78
            $this
79
        ]);
80
81
        return $action->runWithParams([]);
82
    }
83
84
    /**
85
     * @param null $connection
86
     * @return mixed
87
     * @throws \yii\base\InvalidConfigException
88
     */
89
    public function actionUpdate($connection = null)
90
    {
91
        if (null === $connection) {
92
            $connection = Craft::$app->getRequest()->getBodyParam('connection');
93
        }
94
95
        /** @var \yii\base\Action $action */
96
        $action = Craft::createObject([
97
            'class' => UpdateConnection::class
98
        ], [
99
            'update',
100
            $this
101
        ]);
102
103
        return $action->runWithParams([
104
            'connection' => $connection
105
        ]);
106
    }
107
108
    /**
109
     * @param null $connection
110
     * @return mixed
111
     * @throws \yii\base\InvalidConfigException
112
     */
113
    public function actionDelete($connection = null)
114
    {
115
        if (null === $connection) {
116
            $connection = Craft::$app->getRequest()->getBodyParam('connection');
117
        }
118
119
        /** @var DeleteConnection $action */
120
        $action = Craft::createObject([
121
            'class' => DeleteConnection::class
122
        ], [
123
            'delete',
124
            $this
125
        ]);
126
127
        return $action->runWithParams([
128
            'connection' => $connection
129
        ]);
130
    }
131
}
132