Completed
Push — master ( a20c7b...d885ca )
by Nate
02:36
created

Association   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 125
ccs 0
cts 77
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 6 1
A rules() 0 38 1
A beforeSave() 0 12 1
A afterSave() 0 13 1
A afterDelete() 0 13 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-element-lists/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-element-lists/
7
 */
8
9
namespace flipbox\craft\element\lists\records;
10
11
use Craft;
12
use flipbox\craft\ember\records\ActiveRecord;
13
use flipbox\craft\ember\records\FieldAttributeTrait;
14
use flipbox\craft\ember\records\SiteAttributeTrait;
15
use flipbox\craft\ember\records\SortableTrait;
16
use flipbox\craft\element\lists\queries\AssociationQuery;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 *
22
 * @property int $targetId
23
 * @property int $sourceId
24
 */
25
class Association extends ActiveRecord
26
{
27
    use SiteAttributeTrait,
28
        FieldAttributeTrait,
29
        SortableTrait;
30
31
    /**
32
     * @inheritdoc
33
     */
34
    const TABLE_ALIAS = 'elementlist';
35
36
    /**
37
     * @inheritdoc
38
     */
39
    protected $getterPriorityAttributes = ['fieldId', 'siteId'];
40
41
    /**
42
     * @noinspection PhpDocMissingThrowsInspection
43
     * @return AssociationQuery
44
     */
45
    public static function find(): AssociationQuery
46
    {
47
        /** @noinspection PhpIncompatibleReturnTypeInspection */
48
        /** @noinspection PhpUnhandledExceptionInspection */
49
        return Craft::createObject(AssociationQuery::class, [get_called_class()]);
50
    }
51
52
    /*******************************************
53
     * RULES
54
     *******************************************/
55
56
    /**
57
     * @return array
58
     */
59
    public function rules(): array
60
    {
61
        return array_merge(
62
            parent::rules(),
63
            $this->siteRules(),
64
            $this->fieldRules(),
65
            [
66
                [
67
                    [
68
                        'targetId',
69
                        'sourceId',
70
                        'fieldId',
71
                    ],
72
                    'required'
73
                ],
74
                [
75
                    'targetId',
76
                    'unique',
77
                    'targetAttribute' => [
78
                        'targetId',
79
                        'sourceId',
80
                        'fieldId',
81
                        'siteId'
82
                    ]
83
                ],
84
                [
85
                    [
86
                        'targetId',
87
                        'sourceId',
88
                    ],
89
                    'safe',
90
                    'on' => [
91
                        static::SCENARIO_DEFAULT
92
                    ]
93
                ]
94
            ]
95
        );
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function beforeSave($insert)
102
    {
103
        $this->ensureSortOrder(
104
            [
105
                'sourceId' => $this->sourceId,
106
                'fieldId' => $this->fieldId,
107
                'siteId' => $this->siteId
108
            ]
109
        );
110
111
        return parent::beforeSave($insert);
112
    }
113
114
    /**
115
     * @inheritdoc
116
     * @throws \yii\db\Exception
117
     */
118
    public function afterSave($insert, $changedAttributes)
119
    {
120
        $this->autoReOrder(
121
            'targetId',
122
            [
123
                'sourceId' => $this->sourceId,
124
                'fieldId' => $this->fieldId,
125
                'siteId' => $this->siteId
126
            ]
127
        );
128
129
        parent::afterSave($insert, $changedAttributes);
130
    }
131
132
    /**
133
     * @inheritdoc
134
     * @throws \yii\db\Exception
135
     */
136
    public function afterDelete()
137
    {
138
        $this->sequentialOrder(
139
            'targetId',
140
            [
141
                'sourceId' => $this->sourceId,
142
                'fieldId' => $this->fieldId,
143
                'siteId' => $this->siteId
144
            ]
145
        );
146
147
        parent::afterDelete();
148
    }
149
}
150