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

FieldLayoutAttribute   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 0
loc 58
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldLayoutId() 0 9 3
A resolveFieldLayout() 0 8 2
A resolveFieldLayoutFromRelation() 0 8 2
A getFieldLayoutRecord() 0 7 1
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\records\traits;
10
11
use craft\models\FieldLayout as FieldLayoutModel;
12
use craft\records\FieldLayout as FieldLayoutRecord;
13
use flipbox\ember\helpers\FieldLayoutHelper;
14
use flipbox\ember\traits\FieldLayoutMutator;
15
use flipbox\ember\traits\FieldLayoutRules;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait FieldLayoutAttribute
23
{
24
    use ActiveRecord,
25
        FieldLayoutRules,
26
        FieldLayoutMutator;
27
28
    /**
29
     * Get associated fieldLayoutId
30
     *
31
     * @return int|null
32
     */
33
    public function getFieldLayoutId()
34
    {
35
        $fieldLayoutId = $this->getAttribute('fieldLayoutId');
36
        if (null === $fieldLayoutId && null !== $this->fieldLayout) {
37
            $fieldLayoutId = $this->fieldLayoutId = $this->fieldLayout->id;
38
        }
39
40
        return $fieldLayoutId;
41
    }
42
43
    /**
44
     * @return FieldLayoutModel|null
45
     */
46
    protected function resolveFieldLayout()
47
    {
48
        if ($fieldLayout = $this->resolveFieldLayoutFromRelation()) {
49
            return $fieldLayout;
50
        }
51
52
        return $this->resolveFieldLayoutFromId();
53
    }
54
55
    /**
56
     * @return FieldLayoutModel|null|object
57
     */
58
    private function resolveFieldLayoutFromRelation()
59
    {
60
        if (false === $this->isRelationPopulated('fieldLayoutRecord')) {
61
            return null;
62
        }
63
64
        return $this->internalResolveFieldLayout($this->getRelation('fieldLayoutRecord'));
65
    }
66
67
    /**
68
     * Get the associated Field Layout
69
     *
70
     * @return ActiveQueryInterface
71
     */
72
    public function getFieldLayoutRecord()
73
    {
74
        return $this->hasOne(
75
            FieldLayoutRecord::class,
76
            ['fieldLayoutId' => 'id']
77
        );
78
    }
79
}
80