Completed
Push — master ( 04b0bc...360258 )
by Nate
05:02 queued 03:42
created

FieldMutator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 2
dl 0
loc 122
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFieldId() 0 5 1
A getFieldId() 0 8 3
A setField() 0 14 2
A getField() 0 19 4
A resolveField() 0 8 2
A resolveFieldFromId() 0 8 2
A internalResolveField() 0 16 4
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains\records\traits;
10
11
use Craft;
12
use craft\base\Field;
13
use craft\base\FieldInterface;
14
15
/**
16
 * @property int|null $fieldId
17
 * @property Field|FieldInterface|null $field
18
 *
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait FieldMutator
23
{
24
    /**
25
     * @var Field|null
26
     */
27
    private $field;
28
29
    /**
30
     * Set associated fieldId
31
     *
32
     * @param $id
33
     * @return $this
34
     */
35
    public function setFieldId(int $id)
36
    {
37
        $this->fieldId = $id;
38
        return $this;
39
    }
40
41
    /**
42
     * Get associated fieldId
43
     *
44
     * @return int|null
45
     */
46
    public function getFieldId()
47
    {
48
        if (null === $this->fieldId && null !== $this->field) {
49
            $this->fieldId = $this->field->id;
50
        }
51
52
        return $this->fieldId;
53
    }
54
55
    /**
56
     * Associate a field
57
     *
58
     * @param mixed $field
59
     * @return $this
60
     */
61
    public function setField($field = null)
62
    {
63
        $this->field = null;
64
65
        if (!$field = $this->internalResolveField($field)) {
66
            $this->field = $this->fieldId = null;
67
        } else {
68
            /** @var Field $field */
69
            $this->fieldId = $field->id;
70
            $this->field = $field;
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return FieldInterface|null
78
     */
79
    public function getField()
80
    {
81
        /** @var Field $field */
82
        if ($this->field === null) {
83
            $field = $this->resolveField();
84
            $this->setField($field);
85
            return $field;
86
        }
87
88
        $fieldId = $this->fieldId;
89
        if ($fieldId !== null &&
90
            $fieldId !== $this->field->id
91
        ) {
92
            $this->field = null;
93
            return $this->getField();
94
        }
95
96
        return $this->field;
97
    }
98
99
    /**
100
     * @return FieldInterface|null
101
     */
102
    protected function resolveField()
103
    {
104
        if ($model = $this->resolveFieldFromId()) {
105
            return $model;
106
        }
107
108
        return null;
109
    }
110
111
    /**
112
     * @return FieldInterface|null
113
     */
114
    private function resolveFieldFromId()
115
    {
116
        if (null === $this->fieldId) {
117
            return null;
118
        }
119
120
        return Craft::$app->getFields()->getFieldById($this->fieldId);
121
    }
122
123
    /**
124
     * @param mixed $field
125
     * @return FieldInterface|Field|null
126
     */
127
    protected function internalResolveField($field = null)
128
    {
129
        if ($field instanceof FieldInterface) {
130
            return $field;
131
        }
132
133
        if (is_numeric($field)) {
134
            return Craft::$app->getFields()->getFieldById($field);
135
        }
136
137
        if (is_string($field)) {
138
            return Craft::$app->getFields()->getFieldByHandle($field);
139
        }
140
141
        return Craft::$app->getFields()->createField($field);
142
    }
143
}
144