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

FieldsController::actionPerformItemAction()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 30
cp 0
rs 9.0488
c 0
b 0
f 0
cc 5
nc 16
nop 4
crap 30
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;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\craft\integration\actions\fields\CreateFieldItem;
14
use flipbox\craft\integration\actions\fields\PerformFieldAction;
15
use flipbox\craft\integration\actions\fields\PerformFieldItemAction;
16
use yii\web\BadRequestHttpException;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class FieldsController extends AbstractController
23
{
24
    /**
25
     * @return array
26
     */
27
    public function behaviors()
28
    {
29
        return ArrayHelper::merge(
30
            parent::behaviors(),
31
            [
32
                'error' => [
33
                    'default' => 'action'
34
                ],
35
                'redirect' => [
36
                    'only' => ['perform-action'],
37
                    'actions' => [
38
                        'perform-action' => [200]
39
                    ]
40
                ],
41
                'flash' => [
42
                    'actions' => [
43
                        'perform-action' => [
44
                            200 => Craft::t('hubspot', "Action executed successfully."),
45
                            400 => Craft::t('hubspot', "Failed to execute action.")
46
                        ]
47
                    ]
48
                ]
49
            ]
50
        );
51
    }
52
53
    /**
54
     * Executes a Field Action.
55
     *
56
     * @param string|null $field
57
     * @param string|null $element
58
     * @param string|null $action
59
     * @param string|null $id
60
     * @return mixed
61
     * @throws BadRequestHttpException
62
     * @throws \yii\base\InvalidConfigException
63
     */
64
    public function actionPerformItemAction(
65
        string $field = null,
66
        string $element = null,
67
        string $action = null,
68
        string $id = null
69
    ) {
70
        if ($field === null) {
71
            $field = Craft::$app->getRequest()->getRequiredParam('field');
72
        }
73
74
        if ($element === null) {
75
            $element = Craft::$app->getRequest()->getRequiredParam('element');
76
        }
77
78
        if ($action === null) {
79
            $action = Craft::$app->getRequest()->getRequiredParam('action');
80
        }
81
82
        if ($id === null) {
83
            $id = Craft::$app->getRequest()->getRequiredParam('id');
84
        }
85
86
        /** @var PerformFieldItemAction $action */
87
        return (Craft::createObject([
88
            'class' => PerformFieldItemAction::class
89
        ], [
90
            'preform-action',
91
            $this
92
        ]))->runWithParams([
93
            'field' => $field,
94
            'element' => $element,
95
            'action' => $action,
96
            'id' => $id
97
        ]);
98
    }
99
100
    /**
101
     * Executes a Field Action.
102
     *
103
     * @param string|null $field
104
     * @param string|null $element
105
     * @param string|null $action
106
     * @return mixed
107
     * @throws BadRequestHttpException
108
     * @throws \yii\base\InvalidConfigException
109
     */
110
    public function actionPerformAction(
111
        string $field = null,
112
        string $element = null,
113
        string $action = null
114
    ) {
115
        if ($field === null) {
116
            $field = Craft::$app->getRequest()->getRequiredParam('field');
117
        }
118
119
        if ($element === null) {
120
            $element = Craft::$app->getRequest()->getRequiredParam('element');
121
        }
122
123
        if ($action === null) {
124
            $action = Craft::$app->getRequest()->getRequiredParam('action');
125
        }
126
127
        /** @var PerformFieldAction $action */
128
        return (Craft::createObject([
129
            'class' => PerformFieldAction::class
130
        ], [
131
            'preform-action',
132
            $this
133
        ]))->runWithParams([
134
            'field' => $field,
135
            'element' => $element,
136
            'action' => $action
137
        ]);
138
    }
139
140
    /**
141
     * @param string|null $field
142
     * @param string|null $element
143
     * @param string|null $id
144
     * @return mixed
145
     * @throws BadRequestHttpException
146
     * @throws \yii\base\InvalidConfigException
147
     */
148
    public function actionCreateItem(
149
        string $field = null,
150
        string $element = null,
151
        string $id = null
152
    ) {
153
        if ($field === null) {
154
            $field = Craft::$app->getRequest()->getRequiredParam('field');
155
        }
156
157
        if ($element === null) {
158
            $element = Craft::$app->getRequest()->getRequiredParam('element');
159
        }
160
161
        if ($id === null) {
162
            $id = Craft::$app->getRequest()->getParam('id');
163
        }
164
165
        /** @var CreateFieldItem $action */
166
        return (Craft::createObject([
167
            'class' => CreateFieldItem::class
168
        ], [
169
            'create-row',
170
            $this
171
        ]))->runWithParams([
172
            'field' => $field,
173
            'element' => $element,
174
            'id' => $id
175
        ]);
176
    }
177
}
178