Completed
Push — develop ( 5c8ac4...4d88f4 )
by Nate
05:57 queued 04:22
created

OrganizationTypeAssociation::beforeSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\records;
10
11
use Craft;
12
use flipbox\craft\ember\records\ActiveRecord;
13
use flipbox\craft\ember\records\SortableTrait;
14
use flipbox\organizations\queries\OrganizationTypeAssociationQuery;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 *
20
 * @property int $typeId
21
 * @property int $organizationId
22
 * @property int $sortOrder
23
 * @property OrganizationType $type
24
 * @property Organization $organization
25
 */
26
class OrganizationTypeAssociation extends ActiveRecord
27
{
28
    use SortableTrait,
29
        OrganizationTypeAttributeTrait,
30
        OrganizationAttributeTrait;
31
32
    /**
33
     * The table name
34
     */
35
    const TABLE_ALIAS = OrganizationType::TABLE_ALIAS . '_associations';
36
37
    /**
38
     * @inheritdoc
39
     */
40
    protected $getterPriorityAttributes = ['typeId', 'organizationId'];
41
42
    /**
43
     * @noinspection PhpDocMissingThrowsInspection
44
     *
45
     * @inheritdoc
46
     * @return OrganizationTypeAssociationQuery
47
     */
48
    public static function find()
49
    {
50
        /** @noinspection PhpUnhandledExceptionInspection */
51
        /** @noinspection PhpIncompatibleReturnTypeInspection */
52
        return Craft::createObject(OrganizationTypeAssociationQuery::class, [get_called_class()]);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function rules()
59
    {
60
        return array_merge(
61
            parent::rules(),
62
            $this->typeRules(),
63
            $this->organizationRules(),
64
            [
65
                [
66
                    [
67
                        'typeId',
68
                        'organizationId'
69
                    ],
70
                    'required'
71
                ],
72
                [
73
                    [
74
                        'typeId'
75
                    ],
76
                    'unique',
77
                    'targetAttribute' => [
78
                        'typeId',
79
                        'organizationId'
80
                    ]
81
                ]
82
            ]
83
        );
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function beforeSave($insert)
90
    {
91
        $this->ensureSortOrder(
92
            [
93
                'organizationId' => $this->organizationId
94
            ]
95
        );
96
97
        return parent::beforeSave($insert);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     * @throws \yii\db\Exception
103
     */
104
    public function afterSave($insert, $changedAttributes)
105
    {
106
        $this->autoReOrder(
107
            'typeId',
108
            [
109
                'organizationId' => $this->organizationId
110
            ]
111
        );
112
113
        parent::afterSave($insert, $changedAttributes);
114
    }
115
116
    /**
117
     * @inheritdoc
118
     * @throws \yii\db\Exception
119
     */
120
    public function afterDelete()
121
    {
122
        $this->sequentialOrder(
123
            'typeId',
124
            [
125
                'organizationId' => $this->organizationId
126
            ]
127
        );
128
129
        parent::afterDelete();
130
    }
131
}
132