Passed
Push — master ( 30d0f9...a8ef92 )
by Andrey
07:18
created

Technology   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 165
rs 10
c 0
b 0
f 0
wmc 13

11 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 31 1
A attributeLabels() 0 10 1
A tableName() 0 3 1
A attributes() 0 9 1
A scenarios() 0 8 1
A linkWithAbout() 0 11 2
A getAboutTechnologies() 0 4 1
A afterSave() 0 5 2
A getId() 0 3 1
A setAbout() 0 3 1
A getAbout() 0 6 1
1
<?php
2
3
namespace app\models;
4
5
use Itstructure\AdminModule\interfaces\ModelInterface;
6
7
/**
8
 * This is the model class for table "technologies".
9
 *
10
 * @property int $id
11
 * @property string $name
12
 * @property int $share
13
 * @property string $icon
14
 * @property string $created_at
15
 * @property string $updated_at
16
 *
17
 * @property About[] $about
18
 * @property AboutTechnology[] $aboutTechnologies
19
 *
20
 * @package app\models
21
 */
22
class Technology extends ActiveRecord implements ModelInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public static function tableName()
28
    {
29
        return 'technologies';
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function rules()
36
    {
37
        return [
38
            [
39
                [
40
                    'name',
41
                    'share',
42
                    'about'
43
                ],
44
                'required'
45
            ],
46
            [
47
                [
48
                    'share'
49
                ],
50
                'integer'
51
            ],
52
            [
53
                [
54
                    'created_at',
55
                    'updated_at'
56
                ],
57
                'safe'
58
            ],
59
            [
60
                [
61
                    'name',
62
                    'icon'
63
                ],
64
                'string',
65
                'max' => 64
66
            ],
67
        ];
68
    }
69
70
    /**
71
     * List if attributes.
72
     *
73
     * @return array
74
     */
75
    public function attributes()
76
    {
77
        return [
78
            'id',
79
            'name',
80
            'share',
81
            'icon',
82
            'created_at',
83
            'updated_at',
84
        ];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function attributeLabels()
91
    {
92
        return [
93
            'id' => 'ID',
94
            'name' => 'Name',
95
            'share' => 'Share, %',
96
            'icon' => 'Icon',
97
            'about' => 'About',
98
            'created_at' => 'Created At',
99
            'updated_at' => 'Updated At',
100
        ];
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function scenarios()
107
    {
108
        $scenarios = parent::scenarios();
109
110
        $scenarios[self::SCENARIO_CREATE][] = 'about';
111
        $scenarios[self::SCENARIO_UPDATE][] = 'about';
112
113
        return $scenarios;
114
    }
115
116
    /**
117
     * @return \yii\db\ActiveQuery
118
     */
119
    public function getAboutTechnologies()
120
    {
121
        return $this->hasMany(AboutTechnology::class, [
122
            'technologies_id' => 'id'
123
        ]);
124
    }
125
126
    /**
127
     * @return \yii\db\ActiveQuery
128
     */
129
    public function getAbout()
130
    {
131
        return $this->hasMany(About::class, [
132
            'id' => 'about_id'
133
        ])->viaTable('about_technologies', [
134
            'technologies_id' => 'id'
135
        ]);
136
    }
137
138
    /**
139
     * @param $about
140
     *
141
     * @return void
142
     */
143
    public function setAbout($about): void
144
    {
145
        $this->about = $about;
146
    }
147
148
    /**
149
     * Link with about entity after save.
150
     *
151
     * @param bool $insert
152
     * @param array $changedAttributes
153
     */
154
    public function afterSave($insert, $changedAttributes)
155
    {
156
        $this->linkWithAbout(empty($this->about) ? [] : $this->about);
157
158
        parent::afterSave($insert, $changedAttributes);
159
    }
160
161
    /**
162
     * Returns id of the model.
163
     *
164
     * @return int
165
     */
166
    public function getId()
167
    {
168
        return $this->id;
169
    }
170
171
    /**
172
     * Link with about entity.
173
     *
174
     * @param array $aboutList
175
     */
176
    private function linkWithAbout(array $aboutList): void
177
    {
178
        AboutTechnology::deleteAll([
179
            'technologies_id' => $this->id
180
        ]);
181
182
        foreach ($aboutList as $aboutId) {
183
            $aboutTechnologies = new AboutTechnology();
184
            $aboutTechnologies->technologies_id = $this->id;
185
            $aboutTechnologies->about_id = $aboutId;
186
            $aboutTechnologies->save();
187
        }
188
    }
189
}
190