Passed
Push — master ( ad0383...5c48c0 )
by Andrey
06:39
created

Social::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use Itstructure\AdminModule\interfaces\ModelInterface;
7
use Itstructure\AdminModule\models\ActiveRecord;
8
9
/**
10
 * This is the model class for table "social".
11
 *
12
 * @property int $id
13
 * @property string $icon
14
 * @property string $url
15
 * @property string $created_at
16
 * @property string $updated_at
17
 *
18
 * @property ContactSocial[] $contactSocial
19
 * @property Contact[] $contacts
20
 *
21
 * @package app\models
22
 */
23
class Social extends ActiveRecord implements ModelInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public static function tableName()
29
    {
30
        return 'social';
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function rules()
37
    {
38
        return [
39
            [
40
                [
41
                    'icon',
42
                    'url',
43
                    'contacts',
44
                ],
45
                'required'
46
            ],
47
            [
48
                [
49
                    'created_at',
50
                    'updated_at'
51
                ],
52
                'safe'
53
            ],
54
            [
55
                [
56
                    'icon'
57
                ],
58
                'string',
59
                'max' => 128
60
            ],
61
            [
62
                [
63
                    'url'
64
                ],
65
                'string',
66
                'max' => 255
67
            ],
68
        ];
69
    }
70
71
    /**
72
     * List if attributes.
73
     *
74
     * @return array
75
     */
76
    public function attributes()
77
    {
78
        return [
79
            'id',
80
            'icon',
81
            'url',
82
            'created_at',
83
            'updated_at',
84
        ];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function attributeLabels()
91
    {
92
        return [
93
            'id' => 'ID',
94
            'icon' => Yii::t('app', 'Icon html class'),
95
            'url' => Yii::t('social', 'Url'),
96
            'contacts' => Yii::t('contacts', 'Contacts'),
97
            'created_at' => Yii::t('app', 'Created date'),
98
            'updated_at' => Yii::t('app', 'Updated date'),
99
        ];
100
    }
101
102
    /**
103
     * @return array
104
     */
105
    public function scenarios()
106
    {
107
        $scenarios = parent::scenarios();
108
109
        $scenarios[self::SCENARIO_CREATE][] = 'contacts';
110
        $scenarios[self::SCENARIO_UPDATE][] = 'contacts';
111
112
        return $scenarios;
113
    }
114
115
    /**
116
     * @return \yii\db\ActiveQuery
117
     */
118
    public function getContactSocial()
119
    {
120
        return $this->hasMany(ContactSocial::class, [
121
            'social_id' => 'id'
122
        ]);
123
    }
124
125
    /**
126
     * @return \yii\db\ActiveQuery
127
     */
128
    public function getContacts()
129
    {
130
        return $this->hasMany(Contact::class, [
131
            'id' => 'contacts_id'
132
        ])->viaTable('contacts_social', [
133
            'social_id' => 'id'
134
        ]);
135
    }
136
137
    /**
138
     * @param $contacts
139
     *
140
     * @return void
141
     */
142
    public function setContacts($contacts): void
143
    {
144
        $this->contacts = $contacts;
145
    }
146
147
    /**
148
     * Link with contacts entity after save.
149
     *
150
     * @param bool $insert
151
     *
152
     * @param array $changedAttributes
153
     */
154
    public function afterSave($insert, $changedAttributes)
155
    {
156
        $this->linkWithContacts(empty($this->contacts) ? [] : $this->contacts);
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 contacts entity.
173
     *
174
     * @param array $contactList
175
     */
176
    private function linkWithContacts(array $contactList): void
177
    {
178
        ContactSocial::deleteAll([
179
            'social_id' => $this->id
180
        ]);
181
182
        foreach ($contactList as $contactId) {
183
            $contactSocial = new ContactSocial();
184
            $contactSocial->social_id = $this->id;
185
            $contactSocial->contacts_id = $contactId;
186
            $contactSocial->save();
187
        }
188
    }
189
}
190