Completed
Push — develop ( 639fa1...b48ec5 )
by Nate
02:32
created

ObjectFromElementBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 86
c 0
b 0
f 0
ccs 0
cts 43
cp 0
rs 10

4 Methods

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