Completed
Push — master ( fa2880...7af5aa )
by Nate
01:12
created

IntegrationAssociation::afterDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
ccs 0
cts 24
cp 0
rs 9.472
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\helpers\ModelHelper;
13
use flipbox\craft\ember\records\ActiveRecord;
14
use flipbox\craft\ember\records\ElementAttributeTrait;
15
use flipbox\craft\ember\records\FieldAttributeTrait;
16
use flipbox\craft\ember\records\SiteAttributeTrait;
17
use flipbox\craft\ember\records\SortableTrait;
18
use flipbox\craft\integration\queries\IntegrationAssociationQuery;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @property int $fieldId
25
 * @property int $elementId
26
 * @property string $objectId
27
 * @property string $siteId
28
 * @property int $sortOrder
29
 */
30
abstract class IntegrationAssociation extends ActiveRecord
31
{
32
    use SiteAttributeTrait,
33
        ElementAttributeTrait,
34
        FieldAttributeTrait,
35
        SortableTrait;
36
37
    /**
38
     * The default Object Id (if none exists)
39
     */
40
    const DEFAULT_ID = 'UNKNOWN_ID';
41
42
    /**
43
     * @inheritdoc
44
     */
45
    protected $getterPriorityAttributes = ['fieldId', 'elementId', 'siteId'];
46
47
    /**
48
     * @noinspection PhpDocMissingThrowsInspection
49
     * @return IntegrationAssociationQuery
50
     */
51
    public static function find(): IntegrationAssociationQuery
52
    {
53
        /** @noinspection PhpIncompatibleReturnTypeInspection */
54
        /** @noinspection PhpUnhandledExceptionInspection */
55
        return Craft::createObject(IntegrationAssociationQuery::class, [get_called_class()]);
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function rules(): array
62
    {
63
        return array_merge(
64
            parent::rules(),
65
            $this->siteRules(),
66
            $this->elementRules(),
67
            $this->fieldRules(),
68
            [
69
                [
70
                    [
71
                        'objectId',
72
                    ],
73
                    'required'
74
                ],
75
                [
76
                    'objectId',
77
                    'unique',
78
                    'targetAttribute' => [
79
                        'elementId',
80
                        'fieldId',
81
                        'siteId',
82
                        'objectId'
83
                    ]
84
                ],
85
                [
86
                    [
87
                        'objectId'
88
                    ],
89
                    'safe',
90
                    'on' => [
91
                        ModelHelper::SCENARIO_DEFAULT
92
                    ]
93
                ]
94
            ]
95
        );
96
    }
97
98
    /**
99
     * @inheritdoc
100
     */
101
    public function beforeSave($insert)
102
    {
103
        $this->ensureSortOrder(
104
            [
105
                'elementId' => $this->elementId,
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
            'objectId',
122
            [
123
                'elementId' => $this->elementId,
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
        $sortOrderAttribute = 'sortOrder';
139
        $targetAttribute = 'objectId';
140
        $sortOrderCondition = [
141
            'elementId' => $this->elementId,
142
            'fieldId' => $this->fieldId,
143
            'siteId' => $this->siteId
144
        ];
145
146
        // All records (sorted)
147
        $sortOrder = $this->sortOrderQuery($sortOrderCondition, $sortOrderAttribute)
148
            ->indexBy($targetAttribute)
149
            ->select([$sortOrderAttribute])
150
            ->column();
151
152
        $this->saveNewOrder(
153
            array_flip(array_combine(
154
                range($sortOrder, count($sortOrder)),
155
                array_keys($sortOrder)
156
            )),
157
            $targetAttribute,
158
            $sortOrderCondition,
159
            $sortOrderAttribute
160
        );
161
162
        parent::afterDelete();
163
    }
164
}
165