Completed
Push — develop ( 05d944...aa64cd )
by Nate
08:48
created

OrganizationAttributeTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 89
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A organizationAttributes() 0 6 1
A organizationAttributeLabels() 0 6 1
A internalSetOrganizationId() 0 5 1
A internalGetOrganizationId() 0 7 2
A resolveOrganization() 0 8 2
A resolveOrganizationFromRelation() 0 14 3
A getOrganizationRecord() 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\organizations\records;
10
11
use flipbox\craft\ember\records\ActiveRecordTrait;
12
use flipbox\organizations\elements\Organization as OrganizationElement;
13
use flipbox\organizations\models\OrganizationRulesTrait;
14
use flipbox\organizations\objects\OrganizationMutatorTrait;
15
use flipbox\organizations\Organizations;
16
use yii\db\ActiveQueryInterface;
17
18
/**
19
 * Intended to be used on an ActiveRecord, this class provides `$this->organizationId` attribute along with 'getters'
20
 * and 'setters' to ensure continuity between the Id and Object.  An organization object is lazy loaded when called.
21
 * In addition, ActiveRecord rules are available.
22
 *
23
 * @author Flipbox Factory <[email protected]>
24
 * @since 1.0.0
25
 */
26
trait OrganizationAttributeTrait
27
{
28
    use ActiveRecordTrait,
29
        OrganizationRulesTrait,
30
        OrganizationMutatorTrait;
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function organizationAttributes(): array
36
    {
37
        return [
38
            'organizationId'
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function organizationAttributeLabels(): array
46
    {
47
        return [
48
            'organizationId' => Organizations::t('Organization Id')
49
        ];
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    protected function internalSetOrganizationId(int $id = null)
56
    {
57
        $this->setAttribute('organizationId', $id);
58
        return $this;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    protected function internalGetOrganizationId()
65
    {
66
        if (null === ($id = $this->getAttribute('organizationId'))) {
67
            return null;
68
        }
69
        return (int) $id;
70
    }
71
72
    /**
73
     * @return OrganizationElement|null
74
     */
75
    protected function resolveOrganization()
76
    {
77
        if ($model = $this->resolveOrganizationFromRelation()) {
78
            return $model;
79
        }
80
81
        return $this->resolveOrganizationFromId();
82
    }
83
84
    /**
85
     * @return OrganizationElement|null
86
     */
87
    private function resolveOrganizationFromRelation()
88
    {
89
        if (false === $this->isRelationPopulated('organizationRecord')) {
90
            return null;
91
        }
92
93
        if (null === ($record = $this->getRelation('organizationRecord'))) {
94
            return null;
95
        }
96
97
        /** @var Organization $record */
98
99
        return OrganizationElement::findOne($record->id);
100
    }
101
102
    /**
103
     * Returns the associated organization record.
104
     *
105
     * @return ActiveQueryInterface
106
     */
107
    protected function getOrganizationRecord(): ActiveQueryInterface
108
    {
109
        return $this->hasOne(
110
            Organization::class,
111
            ['id' => 'organizationId']
112
        );
113
    }
114
}
115