Organization   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 97
ccs 0
cts 54
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 11 3
B rules() 0 28 1
A getElement() 0 4 1
A getOwner() 0 4 1
A getTypes() 0 5 1
A getUsers() 0 5 1
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\organization\records;
10
11
use craft\base\ElementInterface;
12
use craft\helpers\Db;
13
use craft\records\Element as ElementRecord;
14
use craft\records\User as UserRecord;
15
use flipbox\organization\Organization as OrganizationPlugin;
16
use flipbox\organization\records\OrganizationType as OrganizationTypeRecord;
17
use flipbox\organization\records\User as OrganizationUserRecord;
18
use flipbox\organization\validators\Owner;
19
use flipbox\organization\validators\UserAssociation;
20
use flipbox\spark\records\RecordWithId;
21
use yii\db\ActiveQueryInterface;
22
23
/**
24
 * @author Flipbox Factory <[email protected]>
25
 * @since 1.0.0
26
 *
27
 * @property integer $id
28
 * @property integer $ownerId
29
 * @property string $status
30
 * @property string $dateJoined
31
 * @property ElementInterface $owner
32
 * @property ElementInterface $element
33
 * @property Type[] $types
34
 * @property UserRecord[] $users
35
 */
36
class Organization extends RecordWithId
37
{
38
39
    /**
40
     * The table name
41
     */
42
    const TABLE_ALIAS = 'organizations';
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function beforeSave($insert)
48
    {
49
50
        if ($this->getIsNewRecord()) {
51
            if (!$this->dateJoined) {
52
                $this->dateJoined = Db::prepareDateForDb(new \DateTime());
53
            }
54
        }
55
56
        return parent::beforeSave($insert);
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function rules()
63
    {
64
65
        return array_merge(
66
            parent::rules(),
67
            [
68
                [
69
                    [
70
                        'ownerId'
71
                    ],
72
                    Owner::class
73
                ],
74
                [
75
                    [
76
                        'ownerId'
77
                    ],
78
                    UserAssociation::class
79
                ],
80
                [
81
                    [
82
                        'status'
83
                    ],
84
                    'in',
85
                    'range' => array_keys(OrganizationPlugin::getInstance()->getSettings()->getStatuses())
86
                ]
87
            ]
88
        );
89
    }
90
91
    /**
92
     * Returns the organizations's element.
93
     *
94
     * @return ActiveQueryInterface
95
     */
96
    public function getElement(): ActiveQueryInterface
97
    {
98
        return $this->hasOne(ElementRecord::class, ['id' => 'id']);
99
    }
100
101
    /**
102
     * Returns the organizations's element.
103
     *
104
     * @return ActiveQueryInterface
105
     */
106
    public function getOwner(): ActiveQueryInterface
107
    {
108
        return $this->hasOne(ElementRecord::class, ['id' => 'ownerId']);
109
    }
110
111
    /**
112
     * Returns the organization's types.
113
     *
114
     * @return ActiveQueryInterface
115
     */
116
    public function getTypes(): ActiveQueryInterface
117
    {
118
        return $this->hasMany(Type::class, ['id' => 'typeId'])
119
            ->viaTable(OrganizationTypeRecord::tableName(), ['organizationId' => 'id']);
120
    }
121
122
    /**
123
     * Returns the organizations's users.
124
     *
125
     * @return ActiveQueryInterface
126
     */
127
    public function getUsers(): ActiveQueryInterface
128
    {
129
        return $this->hasMany(UserRecord::class, ['id' => 'userId'])
130
            ->viaTable(OrganizationUserRecord::tableName(), ['organizationId' => 'id']);
131
    }
132
}
133