Completed
Push — develop ( 3f9c00...d25215 )
by Nate
02:35
created

FieldLayoutAttributeTrait::resolveFieldLayout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
 * @property FieldLayoutRecord $fieldLayoutRecord
23
 */
24
trait FieldLayoutAttributeTrait
25
{
26
    use ActiveRecordTrait,
27
        FieldLayoutRulesTrait,
28
        FieldLayoutMutatorTrait {
29
        resolveFieldLayout as parentResolveFieldLayout;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function fieldLayoutAttributes(): array
36
    {
37
        return [
38
            'fieldLayoutId'
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function fieldLayoutAttributeLabels(): array
46
    {
47
        return [
48
            'fieldLayoutId' => Craft::t('app', 'Field Layout Id')
49
        ];
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    protected function internalSetFieldLayoutId(int $id = null)
56
    {
57
        $this->setAttribute('fieldLayoutId', $id);
58
        return $this;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    protected function internalGetFieldLayoutId()
65
    {
66
        if (null === ($id = $this->getAttribute('fieldLayoutId'))) {
67
            return null;
68
        }
69
        return (int)$id;
70
    }
71
72
    /**
73
     * @return FieldLayoutModel|null
74
     */
75
    protected function resolveFieldLayout()
76
    {
77
        if ($fieldLayout = $this->resolveFieldLayoutFromRelation()) {
78
            return $fieldLayout;
79
        }
80
81
        return $this->parentResolveFieldLayout();
1 ignored issue
show
Bug introduced by
The method parentResolveFieldLayout() does not exist on flipbox\craft\ember\reco...eldLayoutAttributeTrait. Did you maybe mean resolveFieldLayout()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
82
    }
83
84
    /**
85
     * @return FieldLayoutModel|null|object
86
     */
87
    private function resolveFieldLayoutFromRelation()
88
    {
89
        if (false === $this->isRelationPopulated('fieldLayoutRecord')) {
90
            return null;
91
        }
92
93
        if (null === ($record = $this->getRelation('fieldLayoutRecord'))) {
94
            return null;
95
        }
96
97
        /** @var FieldLayoutRecord $record */
98
99
        return Craft::$app->getFields()->getLayoutById($record->id);
100
    }
101
102
    /**
103
     * Get the associated Field Layout
104
     *
105
     * @return ActiveQueryInterface
106
     */
107
    public function getFieldLayoutRecord(): ActiveQueryInterface
108
    {
109
        return $this->hasOne(
110
            FieldLayoutRecord::class,
111
            ['id' => 'fieldLayoutId']
112
        );
113
    }
114
}
115