AbstractSyncTo   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 59
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runInternal() 0 15 3
A performAction() 0 6 1
A handleSuccessResponse() 0 6 2
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 AbstractSyncTo extends Action
22
{
23
    use CheckAccessTrait;
24
25
    /**
26
     * @param ElementInterface $element
27
     * @param Objects $field
28
     * @return ElementInterface|mixed
29
     * @throws \yii\web\UnauthorizedHttpException
30
     */
31
    protected function runInternal(
32
        ElementInterface $element,
33
        Objects $field
34
    ) {
35
        // Check access
36
        if (($access = $this->checkAccess($element, $field)) !== true) {
37
            return $access;
38
        }
39
40
        if (false === $this->performAction($element, $field)) {
41
            return $this->handleFailResponse($element);
42
        }
43
44
        return $this->handleSuccessResponse($element);
45
    }
46
47
    /**
48
     * @param ElementInterface $element
49
     * @param Objects $field
50
     * @return bool
51
     */
52
    protected function performAction(
53
        ElementInterface $element,
54
        Objects $field
55
    ) {
56
        return $field->syncToHubSpot($element);
57
    }
58
59
    /**
60
     * @param ElementInterface $element
61
     * @return ElementInterface
62
     */
63
    protected function handleSuccessResponse(ElementInterface $element)
64
    {
65
        // Success status code
66
        Craft::$app->getResponse()->setStatusCode($element ? 200 : 201);
67
        return $element;
68
    }
69
70
    /**
71
     * @param ElementInterface $element
72
     * @return ElementInterface
73
     */
74
    protected function handleFailResponse(ElementInterface $element)
75
    {
76
        Craft::$app->getResponse()->setStatusCode(400);
77
        return $element;
78
    }
79
}
80