IntegrationAssociation::find()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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