Completed
Push — develop ( 2d4f17...d5975b )
by Nate
05:25
created

ConnectionsController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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