Completed
Push — develop ( 3644a8...72e19e )
by Nate
06:59
created

AbstractSyncElementJob::getElement()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 21
cp 0
rs 8.9137
c 0
b 0
f 0
cc 6
nc 8
nop 0
crap 42
1
<?php
2
3
namespace flipbox\hubspot\queue\jobs;
4
5
use Craft;
6
use craft\base\ElementInterface;
7
use craft\db\Query;
8
use craft\elements\db\ElementQuery;
9
use craft\queue\BaseJob;
10
use flipbox\hubspot\fields\Resources;
11
use flipbox\hubspot\HubSpot;
12
use yii\base\Component;
13
use yii\base\InvalidConfigException;
14
15
abstract class AbstractSyncElementJob extends BaseJob
16
{
17
    /**
18
     * @var Resources
19
     */
20
    public $field;
21
22
    /**
23
     * @var ElementInterface
24
     */
25
    public $element;
26
27
    /**
28
     * @var string
29
     */
30
    public $resource;
31
32
    /**
33
     * @return ElementInterface
34
     * @throws InvalidConfigException
35
     */
36
    protected function getElement(): ElementInterface
37
    {
38
        if ($this->isElementInstance($this->element)) {
39
            return $this->element;
40
        }
41
42
        if (is_numeric($this->element)) {
43
            $element = Craft::$app->getElements()->getElementById($this->element);
44
            if ($this->isElementInstance($element)) {
45
                $this->element = $element;
46
                return $this->element;
47
            }
48
        }
49
50
        if (is_string($this->element)) {
51
            $element = Craft::$app->getElements()->getElementByUri($this->element);
52
            if ($this->isElementInstance($element)) {
53
                $this->element = $element;
54
                return $this->element;
55
            }
56
        }
57
58
        throw new InvalidConfigException("Unable to resolve element");
59
    }
60
61
    /**
62
     * @param null $element
63
     * @return bool
64
     */
65
    private function isElementInstance($element = null): bool
66
    {
67
        return $element instanceof ElementInterface;
68
    }
69
70
    /**
71
     * @return Resources
72
     * @throws InvalidConfigException
73
     */
74
    protected function getField(): Resources
75
    {
76
        if ($this->isFieldInstance($this->field)) {
77
            return $this->field;
78
        }
79
80
        if (is_numeric($this->field)) {
81
            $field = Craft::$app->getFields()->getFieldById($this->field);
82
            if ($this->isFieldInstance($field)) {
83
                $this->field = $field;
84
                return $this->field;
85
            }
86
        }
87
88
        if (is_string($this->field)) {
89
            $field = Craft::$app->getFields()->getFieldByHandle($this->field);
90
            if ($this->isFieldInstance($field)) {
91
                $this->field = $field;
92
                return $this->field;
93
            }
94
        }
95
96
        throw new InvalidConfigException("Unable to resolve field");
97
    }
98
99
    /**
100
     * @param null $field
101
     * @return bool
102
     */
103
    private function isFieldInstance($field = null): bool
104
    {
105
        return $field instanceof Resources;
106
    }
107
108
    /**
109
     * @return Component
110
     * @throws InvalidConfigException
111
     */
112
    protected function getResource(): Component
113
    {
114
        return HubSpot::getInstance()->getResources()->get($this->resource);
115
    }
116
}