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

WidgetsController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 70
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 25 1
A actionSyncFrom() 0 29 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\craft\hubspot\cp\controllers;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\helpers\ArrayHelper;
14
use flipbox\craft\hubspot\cp\actions\widgets\SyncFrom;
15
use yii\web\HttpException;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class WidgetsController 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' => 'criteria'
33
                ],
34
                'redirect' => [
35
                    'only' => ['sync-from'],
36
                    'actions' => [
37
                        'sync-from' => [200]
38
                    ]
39
                ],
40
                'flash' => [
41
                    'actions' => [
42
                        'sync-from' => [
43
                            200 => Craft::t('hubspot', "HubSpot Object synced successfully"),
44
                            400 => Craft::t('hubspot', "Failed to sync HubSpot Object")
45
                        ]
46
                    ]
47
                ]
48
            ]
49
        );
50
    }
51
52
    /**
53
     * @param string|null $id
54
     * @param string|null $field
55
     * @param string|null $elementType
56
     * @return ElementInterface|null
57
     * @throws HttpException
58
     * @throws \yii\base\InvalidConfigException
59
     * @throws \yii\web\BadRequestHttpException
60
     */
61
    public function actionSyncFrom(
62
        string $id = null,
63
        string $field = null,
64
        string $elementType = null
65
    ) {
66
        if ($id === null) {
67
            $id = Craft::$app->getRequest()->getRequiredParam('id');
68
        }
69
70
        if ($field === null) {
71
            $field = Craft::$app->getRequest()->getRequiredParam('field');
72
        }
73
74
        if ($elementType === null) {
75
            $elementType = Craft::$app->getRequest()->getRequiredParam('elementType');
76
        }
77
78
        /** @var SyncFrom $action */
79
        return (Craft::createObject([
80
            'class' => SyncFrom::class
81
        ], [
82
            'sync-from',
83
            $this
84
        ]))->runWithParams([
85
            'id' => $id,
86
            'field' => $field,
87
            'elementType' => $elementType
88
        ]);
89
    }
90
}
91