Completed
Push — develop ( de31bd...3158f9 )
by Nate
20:35
created

ObjectFromElementAccessor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 8
dl 0
loc 75
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getElement() 0 16 4
A getField() 0 19 5
A getId() 0 7 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\hubspot\criteria\element;
10
11
use Craft;
12
use craft\base\ElementInterface;
13
use craft\errors\ElementNotFoundException;
14
use craft\errors\FieldNotFoundException;
15
use flipbox\hubspot\criteria\ObjectAccessorInterface;
16
use flipbox\hubspot\criteria\traits\CacheTrait;
17
use flipbox\hubspot\criteria\traits\ConnectionTrait;
18
use flipbox\hubspot\criteria\traits\TransformerCollectionTrait;
19
use flipbox\hubspot\fields\Objects;
20
use flipbox\hubspot\traits\TransformElementIdTrait;
21
use yii\base\BaseObject;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 */
27
class ObjectFromElementAccessor extends BaseObject implements ObjectAccessorInterface
28
{
29
    use TransformElementIdTrait,
30
        TransformerCollectionTrait,
31
        ConnectionTrait,
32
        CacheTrait;
33
34
    /**
35
     * @var ElementInterface
36
     */
37
    public $element;
38
39
    /**
40
     * @var Objects
41
     */
42
    public $field;
43
44
    /**
45
     * @return ElementInterface
46
     * @throws ElementNotFoundException
47
     */
48
    protected function getElement(): ElementInterface
49
    {
50
        if ($this->element instanceof ElementInterface) {
51
            return $this->element;
52
        }
53
54
        if (is_numeric($this->element)) {
55
            return $this->element = Craft::$app->getElements()->getElementById($this->element);
56
        }
57
58
        if (is_string($this->element)) {
59
            return $this->element = Craft::$app->getElements()->getElementByUri($this->element);
60
        }
61
62
        throw new ElementNotFoundException("Unable to resolve element.");
63
    }
64
65
    /**
66
     * @return Objects
67
     * @throws FieldNotFoundException
68
     */
69
    protected function getField(): Objects
70
    {
71
        if ($this->field instanceof Objects) {
72
            return $this->field;
73
        }
74
75
        $field = null;
76
        if (is_numeric($this->field)) {
77
            $field = Craft::$app->getFields()->getFieldById($this->field);
78
        } elseif (is_string($this->element)) {
79
            $field = Craft::$app->getFields()->getFieldByHandle($this->field);
80
        }
81
82
        if ($field instanceof Objects) {
83
            return $this->field = $field;
84
        }
85
86
        throw new FieldNotFoundException("Unable to resolve field.");
87
    }
88
89
    /**
90
     * @inheritdoc
91
     * @throws FieldNotFoundException
92
     * @throws ElementNotFoundException
93
     */
94
    public function getId(): string
95
    {
96
        return $this->transformElementId(
97
            $this->getElement(),
98
            $this->getField()
99
        );
100
    }
101
}
102