Completed
Push — master ( 80fd4c...ece9a3 )
by Paweł
03:01
created

Account::attributeHints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace app\models;
4
5
use Yii;
6
use yii\behaviors\AttributeBehavior;
7
use yii\behaviors\TimestampBehavior;
8
use yii\db\BaseActiveRecord;
9
use yii\db\Expression;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * This is the model class for table "account".
14
 *
15
 * @property int $id
16
 * @property string $uid
17
 * @property string $name
18
 * @property string $username
19
 * @property string $profile_pic_url
20
 * @property string $full_name
21
 * @property string $biography
22
 * @property string $external_url
23
 * @property string $instagram_id
24
 * @property string $updated_at
25
 * @property string $created_at
26
 * @property bool $monitoring
27
 * @property int $proxy_id
28
 * @property int $proxy_tag_id
29
 * @property string $notes
30
 * @property bool $disabled
31
 * @property int $accounts_monitoring_level
32
 *
33
 * @property string $usernamePrefixed
34
 * @property string $displayName
35
 *
36
 * @property AccountStats $lastAccountStats
37
 *
38
 * @property Proxy $proxy
39
 * @property Tag $proxyTag
40
 * @property AccountStats[] $accountStats
41
 * @property AccountTag[] $accountTags
42
 * @property Tag[] $tags
43
 * @property Media[] $media
44
 * @property MediaAccount[] $mediaAccounts
45
 */
46
class Account extends \yii\db\ActiveRecord
47
{
48
    public $occurs;
49
50
    public static function usedTags()
51
    {
52
        return Tag::find()
53
            ->innerJoin('account_tag', 'tag.id=account_tag.tag_id')
54
            ->orderBy('tag.slug ASC')
55
            ->all();
56
    }
57
58
    public function behaviors()
59
    {
60
        return ArrayHelper::merge(parent::behaviors(), [
61
            'time' => TimestampBehavior::class,
62
            'uid' => [
63
                'class' => AttributeBehavior::class,
64
                'attributes' => [
65
                    BaseActiveRecord::EVENT_BEFORE_INSERT => ['uid'],
66
                    BaseActiveRecord::EVENT_BEFORE_UPDATE => ['uid'],
67
                ],
68
                'preserveNonEmptyValues' => true,
69
                'value' => function () {
70
                    do {
71
                        $uid = Yii::$app->security->generateRandomString(64);
72
                        $uidExist = static::find()
73
                            ->andWhere(['account.uid' => $uid])
74
                            ->exists();
75
                    } while ($uidExist);
76
77
                    return $uid;
78
                },
79
            ],
80
        ]);
81
    }
82
83
    public function getDisplayName()
84
    {
85
        return $this->name ?: $this->getUsernamePrefixed();
86
    }
87
88
    public function getUsernamePrefixed()
89
    {
90
        return "@{$this->username}";
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public static function tableName()
97
    {
98
        return 'account';
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function rules()
105
    {
106
        return [
107
            [['username'], 'required'],
108
            [['updated_at', 'created_at'], 'safe'],
109
            [['proxy_id', 'proxy_tag_id', 'occurs'], 'integer'],
110
            ['accounts_monitoring_level', 'integer', 'min' => 0],
111
            [['name', 'username', 'profile_pic_url', 'full_name', 'biography', 'external_url', 'instagram_id', 'notes', 'uid'], 'string', 'max' => 255],
112
            [['monitoring', 'disabled'], 'boolean'],
113
            [['username'], 'unique'],
114
            [['proxy_id'], 'exist', 'skipOnError' => true, 'targetClass' => Proxy::class, 'targetAttribute' => ['proxy_id' => 'id']],
115
            [['proxy_tag_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tag::class, 'targetAttribute' => ['proxy_tag_id' => 'id']],
116
        ];
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122
    public function attributeLabels()
123
    {
124
        return [
125
            'id' => 'ID',
126
            'name' => 'Name',
127
            'username' => 'Username',
128
            'profile_pic_url' => 'Profile Pic Url',
129
            'full_name' => 'Full Name',
130
            'biography' => 'Biography',
131
            'external_url' => 'External Url',
132
            'instagram_id' => 'Instagram ID',
133
            'updated_at' => 'Updated At',
134
            'created_at' => 'Created At',
135
            'monitoring' => 'Monitoring',
136
            'proxy_id' => 'Proxy ID',
137
            'proxy_tag_id' => 'Proxy Tag ID',
138
            'notes' => 'Notes',
139
            'accounts_monitoring_level' => 'Accounts Monitoring Level',
140
        ];
141
    }
142
143
    public function attributeHints()
144
    {
145
        return [
146
            'name' => 'The name displayed in the lists, if empty, the \'username\' will be used.',
147
            'accounts_monitoring_level' => 'Automatically monitors discovered accounts. Be careful.',
148
        ];
149
    }
150
151
    /**
152
     * @return \yii\db\ActiveQuery
153
     */
154
    public function getProxy()
155
    {
156
        return $this->hasOne(Proxy::class, ['id' => 'proxy_id']);
157
    }
158
159
    /**
160
     * @return \yii\db\ActiveQuery
161
     */
162
    public function getProxyTag()
163
    {
164
        return $this->hasOne(Tag::class, ['id' => 'proxy_tag_id']);
165
    }
166
167
    /**
168
     * @return \yii\db\ActiveQuery
169
     */
170
    public function getAccountStats()
171
    {
172
        return $this->hasMany(AccountStats::class, ['account_id' => 'id']);
173
    }
174
175
    /**
176
     * @return \yii\db\ActiveQuery
177
     */
178
    public function getLastAccountStats()
179
    {
180
        return $this->hasOne(AccountStats::class, ['account_id' => 'id'])
181
            ->orderBy('account_stats.id DESC')
182
            ->limit(1);
183
    }
184
185
    /**
186
     * @return \yii\db\ActiveQuery
187
     */
188
    public function getAccountTags()
189
    {
190
        return $this->hasMany(AccountTag::class, ['account_id' => 'id']);
191
    }
192
193
    /**
194
     * @return \yii\db\ActiveQuery
195
     */
196
    public function getTags()
197
    {
198
        return $this->hasMany(Tag::class, ['id' => 'tag_id'])->via('accountTags');
199
    }
200
201
    /**
202
     * @return \yii\db\ActiveQuery
203
     */
204
    public function getMedia()
205
    {
206
        return $this->hasMany(Media::class, ['account_id' => 'id']);
207
    }
208
209
    /**
210
     * @return \yii\db\ActiveQuery
211
     */
212
    public function getMediaAccounts()
213
    {
214
        return $this->hasMany(MediaAccount::class, ['account_id' => 'id']);
215
    }
216
217
    /**
218
     * @inheritdoc
219
     * @return AccountQuery the active query used by this AR class.
220
     */
221
    public static function find()
222
    {
223
        return new AccountQuery(get_called_class());
224
    }
225
}
226