AbstractSyncFrom::runInternal()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 12
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