ObjectsController::actionDissociate()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 25
cp 0
rs 9.44
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 20
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\hubspot\actions\objects\AssociateObject;
14
use flipbox\craft\hubspot\actions\objects\DissociateObject;
15
use flipbox\craft\hubspot\HubSpot;
16
use flipbox\craft\hubspot\records\ObjectAssociation;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class ObjectsController 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' => 'element'
34
                ],
35
                'redirect' => [
36
                    'only' => ['associate', 'dissociate'],
37
                    'actions' => [
38
                        'associate' => [200],
39
                        'dissociate' => [200]
40
                    ]
41
                ],
42
                'flash' => [
43
                    'actions' => [
44
                        'associate' => [
45
                            200 => HubSpot::t("HubSpot Object associated successfully"),
46
                            400 => HubSpot::t("Failed to associate HubSpot Object")
47
                        ],
48
                        'dissociate' => [
49
                            200 => HubSpot::t("HubSpot Object dissociated successfully"),
50
                            400 => HubSpot::t("Failed to dissociate HubSpot Object")
51
                        ]
52
                    ]
53
                ]
54
            ]
55
        );
56
    }
57
58
    /**
59
     * @param string|null $newObjectId
60
     * @param string|null $objectId
61
     * @param string|null $field
62
     * @param string|null $element
63
     * @return ObjectAssociation|null
64
     * @throws \yii\base\InvalidConfigException
65
     * @throws \yii\web\BadRequestHttpException
66
     */
67
    public function actionAssociate(
68
        string $newObjectId = null,
69
        string $objectId = null,
70
        string $field = null,
71
        string $element = null
72
    ) {
73
74
        if ($objectId === null) {
75
            $objectId = Craft::$app->getRequest()->getParam('objectId');
76
        }
77
78
        if ($newObjectId === null) {
79
            $newObjectId = Craft::$app->getRequest()->getRequiredParam('newObjectId');
80
        }
81
82
        if ($field === null) {
83
            $field = Craft::$app->getRequest()->getRequiredParam('field');
84
        }
85
86
        if ($element === null) {
87
            $element = Craft::$app->getRequest()->getRequiredParam('element');
88
        }
89
90
        /** @var AssociateObject $action */
91
        return (Craft::createObject([
92
            'class' => AssociateObject::class
93
        ], [
94
            'associate',
95
            $this
96
        ]))->runWithParams([
97
            'field' => $field,
98
            'element' => $element,
99
            'objectId' => $objectId,
100
            'newObjectId' => $newObjectId
101
        ]);
102
    }
103
104
    /**
105
     * @param string|null $objectId
106
     * @param string|null $field
107
     * @param string|null $element
108
     * @return ObjectAssociation|null
109
     * @throws \yii\base\InvalidConfigException
110
     * @throws \yii\web\BadRequestHttpException
111
     */
112
    public function actionDissociate(
113
        string $objectId = null,
114
        string $field = null,
115
        string $element = null
116
    ) {
117
118
        if ($objectId === null) {
119
            $objectId = Craft::$app->getRequest()->getRequiredParam('objectId');
120
        }
121
122
        if ($field === null) {
123
            $field = Craft::$app->getRequest()->getRequiredParam('field');
124
        }
125
126
        if ($element === null) {
127
            $element = Craft::$app->getRequest()->getRequiredParam('element');
128
        }
129
130
        /** @var DissociateObject $action */
131
        return (Craft::createObject([
132
            'class' => DissociateObject::class
133
        ], [
134
            'dissociate',
135
            $this
136
        ]))->runWithParams([
137
            'field' => $field,
138
            'element' => $element,
139
            'objectId' => $objectId
140
        ]);
141
    }
142
}
143