Completed
Push — master ( f8c4bd...25c22e )
by Nate
09:23 queued 07:32
created

ObjectsController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 121
ccs 0
cts 85
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 30 1
A actionAssociate() 0 36 5
A actionDissociate() 0 30 4
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\hubspot\cp\controllers;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\hubspot\actions\resources\Associate;
14
use flipbox\hubspot\actions\resources\Dissociate;
15
use flipbox\hubspot\records\ObjectAssociation;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class ObjectsController extends AbstractController
22
{
23
    /**
24
     * @return array
25
     */
26
    public function behaviors()
27
    {
28
        return ArrayHelper::merge(
29
            parent::behaviors(),
30
            [
31
                'error' => [
32
                    'default' => 'element'
33
                ],
34
                'redirect' => [
35
                    'only' => ['associate', 'dissociate'],
36
                    'actions' => [
37
                        'associate' => [200],
38
                        'dissociate' => [200]
39
                    ]
40
                ],
41
                'flash' => [
42
                    'actions' => [
43
                        'associate' => [
44
                            200 => Craft::t('hubspot', "HubSpot Object associated successfully"),
45
                            400 => Craft::t('hubspot', "Failed to associate HubSpot Object")
46
                        ],
47
                        'dissociate' => [
48
                            200 => Craft::t('hubspot', "HubSpot Object dissociated successfully"),
49
                            400 => Craft::t('hubspot', "Failed to dissociate HubSpot Object")
50
                        ]
51
                    ]
52
                ]
53
            ]
54
        );
55
    }
56
57
    /**
58
     * @param string|null $newObjectId
59
     * @param string|null $objectId
60
     * @param string|null $field
61
     * @param string|null $element
62
     * @return ObjectAssociation|null
63
     * @throws \yii\base\InvalidConfigException
64
     * @throws \yii\web\BadRequestHttpException
65
     */
66
    public function actionAssociate(
67
        string $newObjectId = null,
68
        string $objectId = null,
69
        string $field = null,
70
        string $element = null
71
    ) {
72
73
        if ($objectId === null) {
74
            $objectId = Craft::$app->getRequest()->getParam('objectId');
75
        }
76
77
        if ($newObjectId === null) {
78
            $newObjectId = Craft::$app->getRequest()->getRequiredParam('newObjectId');
79
        }
80
81
        if ($field === null) {
82
            $field = Craft::$app->getRequest()->getRequiredParam('field');
83
        }
84
85
        if ($element === null) {
86
            $element = Craft::$app->getRequest()->getRequiredParam('element');
87
        }
88
89
        /** @var Associate $action */
90
        return (Craft::createObject([
91
            'class' => Associate::class
92
        ], [
93
            'associate',
94
            $this
95
        ]))->runWithParams([
96
            'field' => $field,
97
            'element' => $element,
98
            'objectId' => $objectId,
99
            'newObjectId' => $newObjectId
100
        ]);
101
    }
102
103
    /**
104
     * @param string|null $objectId
105
     * @param string|null $field
106
     * @param string|null $element
107
     * @return ObjectAssociation|null
108
     * @throws \yii\base\InvalidConfigException
109
     * @throws \yii\web\BadRequestHttpException
110
     */
111
    public function actionDissociate(
112
        string $objectId = null,
113
        string $field = null,
114
        string $element = null
115
    ) {
116
117
        if ($objectId === null) {
118
            $objectId = Craft::$app->getRequest()->getRequiredParam('objectId');
119
        }
120
121
        if ($field === null) {
122
            $field = Craft::$app->getRequest()->getRequiredParam('field');
123
        }
124
125
        if ($element === null) {
126
            $element = Craft::$app->getRequest()->getRequiredParam('element');
127
        }
128
129
        /** @var Dissociate $action */
130
        return (Craft::createObject([
131
            'class' => Dissociate::class
132
        ], [
133
            'dissociate',
134
            $this
135
        ]))->runWithParams([
136
            'field' => $field,
137
            'element' => $element,
138
            'objectId' => $objectId
139
        ]);
140
    }
141
}
142