AbstractSyncFrom   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 68
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runInternal() 0 16 3
A performAction() 0 7 1
A handleSuccessResponse() 0 5 1
A handleFailResponse() 0 5 1
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\actions\sync;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use flipbox\craft\ember\actions\CheckAccessTrait;
14
use flipbox\craft\hubspot\fields\Objects;
15
use yii\base\Action;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
abstract class AbstractSyncFrom extends Action
22
{
23
    use CheckAccessTrait;
24
25
    /**
26
     * @param ElementInterface $element
27
     * @param Objects $field
28
     * @param string|null $objectId
29
     * @return ElementInterface|mixed
30
     * @throws \Throwable
31
     * @throws \craft\errors\ElementNotFoundException
32
     * @throws \yii\base\Exception
33
     * @throws \yii\web\UnauthorizedHttpException
34
     */
35
    protected function runInternal(
36
        ElementInterface $element,
37
        Objects $field,
38
        string $objectId = null
39
    ) {
40
        // Check access
41
        if (($access = $this->checkAccess($element, $field, $objectId)) !== true) {
42
            return $access;
43
        }
44
45
        if (false === $this->performAction($element, $field, $objectId)) {
46
            return $this->handleFailResponse($element);
47
        }
48
49
        return $this->handleSuccessResponse($element);
50
    }
51
52
    /**
53
     * @param ElementInterface $element
54
     * @param Objects $field
55
     * @param string|null $objectId
56
     * @return bool
57
     * @throws \Throwable
58
     * @throws \craft\errors\ElementNotFoundException
59
     * @throws \yii\base\Exception
60
     */
61
    protected function performAction(
62
        ElementInterface $element,
63
        Objects $field,
64
        string $objectId = null
65
    ) {
66
        return $field->syncFromHubSpot($element, $objectId);
67
    }
68
69
    /**
70
     * @param ElementInterface $element
71
     * @return ElementInterface
72
     */
73
    protected function handleSuccessResponse(ElementInterface $element)
74
    {
75
        Craft::$app->getResponse()->setStatusCode(200);
76
        return $element;
77
    }
78
79
    /**
80
     * @param ElementInterface $element
81
     * @return ElementInterface
82
     */
83
    protected function handleFailResponse(ElementInterface $element)
84
    {
85
        Craft::$app->getResponse()->setStatusCode(400);
86
        return $element;
87
    }
88
}
89