Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

resolveFieldLayoutFromRelation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 10
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
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\craft\ember\records;
10
11
use Craft;
12
use craft\models\FieldLayout as FieldLayoutModel;
13
use craft\records\FieldLayout as FieldLayoutRecord;
14
use flipbox\craft\ember\models\FieldLayoutRulesTrait;
15
use flipbox\craft\ember\objects\FieldLayoutMutatorTrait;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 2.0.0
21
 */
22
trait FieldLayoutAttributeTrait
23
{
24
    use ActiveRecordTrait,
25
        FieldLayoutRulesTrait,
26
        FieldLayoutMutatorTrait;
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function fieldLayoutAttributes(): array
32
    {
33
        return [
34
            'fieldLayoutId'
35
        ];
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function fieldLayoutAttributeLabels(): array
42
    {
43
        return [
44
            'fieldLayoutId' => Craft::t('app', 'Field Layout Id')
45
        ];
46
    }
47
48
    /**
49
     * Get associated fieldLayoutId
50
     *
51
     * @return int|null
52
     */
53
    public function getFieldLayoutId()
54
    {
55
        $fieldLayoutId = $this->getAttribute('fieldLayoutId');
56
        if (null === $fieldLayoutId && null !== $this->fieldLayout) {
57
            $fieldLayoutId = $this->fieldLayoutId = $this->fieldLayout->id;
58
        }
59
60
        return $fieldLayoutId;
61
    }
62
63
    /**
64
     * @return FieldLayoutModel|null
65
     */
66
    protected function resolveFieldLayout()
67
    {
68
        if ($fieldLayout = $this->resolveFieldLayoutFromRelation()) {
69
            return $fieldLayout;
70
        }
71
72
        return $this->resolveFieldLayoutFromId();
73
    }
74
75
    /**
76
     * @return FieldLayoutModel|null|object
77
     */
78
    private function resolveFieldLayoutFromRelation()
79
    {
80
        if (false === $this->isRelationPopulated('fieldLayoutRecord')) {
81
            return null;
82
        }
83
84
        if (null === ($record = $this->getRelation('fieldLayoutRecord'))) {
85
            return null;
86
        }
87
88
        /** @var FieldLayoutRecord $record */
89
90
        return Craft::$app->getFields()->getLayoutById($record->id);
91
    }
92
93
    /**
94
     * Get the associated Field Layout
95
     *
96
     * @return ActiveQueryInterface
97
     */
98
    public function getFieldLayoutRecord()
99
    {
100
        return $this->hasOne(
101
            FieldLayoutRecord::class,
102
            ['fieldLayoutId' => 'id']
103
        );
104
    }
105
}
106