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

FieldAttribute   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 0
loc 64
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldId() 0 9 3
A resolveField() 0 8 2
A resolveFieldFromRelation() 0 14 3
A getFieldRecord() 0 7 1
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\FieldInterface;
13
use craft\records\Field as FieldRecord;
14
use flipbox\ember\records\traits\ActiveRecord;
15
use yii\db\ActiveQueryInterface;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
trait FieldAttribute
22
{
23
    use ActiveRecord,
24
        FieldRules,
25
        FieldMutator;
26
27
    /**
28
     * Get associated fieldId
29
     *
30
     * @return int|null
31
     */
32
    public function getFieldId()
33
    {
34
        $id = $this->getAttribute('fieldId');
35
        if (null === $id && null !== $this->field) {
36
            $id = $this->fieldId = $this->field->id;
37
        }
38
39
        return $id;
40
    }
41
42
    /**
43
     * @return FieldInterface|null
44
     */
45
    protected function resolveField()
46
    {
47
        if ($model = $this->resolveFieldFromRelation()) {
48
            return $model;
49
        }
50
51
        return $this->resolveFieldFromId();
52
    }
53
54
    /**
55
     * @return FieldInterface|null
56
     */
57
    private function resolveFieldFromRelation()
58
    {
59
        if (false === $this->isRelationPopulated('fieldRecord')) {
60
            return null;
61
        }
62
63
        /** @var FieldRecord $record */
64
        $record = $this->getRelation('fieldRecord');
65
        if (null === $record) {
66
            return null;
67
        }
68
69
        return Craft::$app->getFields()->getFieldById($record->id);
70
    }
71
72
    /**
73
     * Get the associated Field
74
     *
75
     * @return ActiveQueryInterface
76
     */
77
    public function getFieldRecord()
78
    {
79
        return $this->hasOne(
80
            FieldRecord::class,
81
            ['fieldId' => 'id']
82
        );
83
    }
84
}
85