Completed
Push — master ( 69ca06...8af5d6 )
by Nate
03:36
created

FieldLayoutMutator::internalResolveFieldLayout()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 22
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
crap 30
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\traits;
10
11
use Craft;
12
use craft\models\FieldLayout;
13
use flipbox\ember\helpers\ObjectHelper;
14
15
/**
16
 * @property int|null $fieldLayoutId
17
 * @property FieldLayout|null $fieldLayout
18
 *
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait FieldLayoutMutator
23
{
24
    /**
25
     * @var FieldLayout|null
26
     */
27
    private $fieldLayout;
28
29
    /**
30
     * @return string
31
     */
32
    abstract static protected function fieldLayoutType(): string;
33
34
    /**
35
     * Set associated fieldLayoutId
36
     *
37
     * @param $id
38
     * @return $this
39
     */
40
    public function setFieldLayoutId(int $id)
41
    {
42
        $this->fieldLayoutId = $id;
43
        return $this;
44
    }
45
46
    /**
47
     * Get associated fieldLayoutId
48
     *
49
     * @return int|null
50
     */
51
    public function getFieldLayoutId()
52
    {
53
        if (null === $this->fieldLayoutId && null !== $this->fieldLayout) {
54
            $this->fieldLayoutId = $this->fieldLayout->id;
55
        }
56
57
        return $this->fieldLayoutId;
58
    }
59
60
    /**
61
     * Associate a fieldLayout
62
     *
63
     * @param $fieldLayout
64
     * @return $this
65
     */
66
    public function setFieldLayout($fieldLayout)
67
    {
68
        $this->fieldLayout = null;
69
70
        if (!$fieldLayout = $this->internalResolveFieldLayout($fieldLayout)) {
71
            $this->fieldLayout = $this->fieldLayoutId = null;
72
        } else {
73
            $this->fieldLayoutId = $fieldLayout->id;
74
            $this->fieldLayout = $fieldLayout;
75
        }
76
77
        return $this;
78
    }
79
80
    /**
81
     * @return FieldLayout|null
82
     */
83
    public function getFieldLayout(): FieldLayout
84
    {
85
        if ($this->fieldLayout === null) {
86
            if (!$fieldLayout = $this->resolveFieldLayout()) {
87
                $fieldLayout = $this->createFieldLayout();
88
            }
89
90
            $this->setFieldLayout($fieldLayout);
91
            return $this->setTypeOnFieldLayout($fieldLayout);
92
        }
93
94
        $fieldLayoutId = $this->fieldLayoutId;
95
        if ($fieldLayoutId !== null &&
96
            $fieldLayoutId !== $this->fieldLayout->id
97
        ) {
98
            $this->fieldLayout = null;
99
            return $this->getFieldLayout();
100
        }
101
102
        return $this->setTypeOnFieldLayout($this->fieldLayout);
103
    }
104
105
    /**
106
     * @param FieldLayout $fieldLayout
107
     * @return FieldLayout
108
     */
109
    protected function setTypeOnFieldLayout(FieldLayout $fieldLayout): FieldLayout
110
    {
111
        if ($fieldLayout->type === null) {
112
            $fieldLayout->type = static::fieldLayoutType();
113
        }
114
115
        return $fieldLayout;
116
    }
117
118
    /**
119
     * @return FieldLayout|null
120
     */
121
    protected function resolveFieldLayout()
122
    {
123
        if ($fieldLayoutModel = $this->resolveFieldLayoutFromId()) {
124
            return $fieldLayoutModel;
125
        }
126
127
        return $this->resolveFieldLayoutFromType();
128
    }
129
130
    /**
131
     * @return FieldLayout|null
132
     */
133
    private function resolveFieldLayoutFromId()
134
    {
135
        if (null === $this->fieldLayoutId) {
136
            return null;
137
        }
138
139
        return Craft::$app->getFields()->getLayoutById($this->fieldLayoutId);
140
    }
141
142
    /**
143
     * @return FieldLayout|null
144
     */
145
    private function resolveFieldLayoutFromType()
146
    {
147
        return Craft::$app->getFields()->getLayoutByType(
148
            static::fieldLayoutType()
149
        );
150
    }
151
152
    /**
153
     * @return FieldLayout
154
     */
155
    private function createFieldLayout(): FieldLayout
156
    {
157
        $fieldLayoutModel = new FieldLayout([
158
            'type' => $this->fieldLayoutType()
159
        ]);
160
161
        return $fieldLayoutModel;
162
    }
163
164
    /**
165
     * @param $fieldLayout
166
     * @return FieldLayout|null
167
     */
168
    protected function internalResolveFieldLayout($fieldLayout = null): FieldLayout
169
    {
170
        if ($fieldLayout instanceof FieldLayout) {
171
            return $fieldLayout;
172
        }
173
174
        if (is_numeric($fieldLayout)) {
175
            return Craft::$app->getFields()->getLayoutById($fieldLayout);
176
        }
177
178
        if (is_string($fieldLayout)) {
179
            return Craft::$app->getFields()->getLayoutByType($fieldLayout);
180
        }
181
182
        try {
183
            $object = Craft::createObject(FieldLayout::class, [$fieldLayout]);
184
        } catch (\Exception $e) {
185
            $object = new FieldLayout();
186
            ObjectHelper::populate(
187
                $object,
188
                $fieldLayout
189
            );
190
        }
191
192
        /** @var FieldLayout $object */
193
        return $object;
194
    }
195
}
196