Completed
Push — master ( 49f7b7...3b2b8f )
by vistart
26:05
created

Profile::getCacheTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use rhosocial\base\models\models\BaseBlameableModel;
16
use Yii;
17
use yii\base\Event;
18
use yii\caching\TagDependency;
19
20
/**
21
 * Simple Profile Model.
22
 * One Profile corresponds to only one [[User]].
23
 *
24
 * If you're using MySQL, we recommend that you create a data table using the following statement:
25
```SQL
26
CREATE TABLE `profile` (
27
  `guid` varbinary(16) NOT NULL COMMENT 'User GUID',
28
  `nickname` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Nickname',
29
  `first_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'First Name',
30
  `last_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Last Name',
31
  `gravatar_type` smallint(6) NOT NULL DEFAULT '0' COMMENT 'Gravatar Type',
32
  `gravatar` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT 'Gravatar',
33
  `gender` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Gender',
34
  `timezone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'UTC' COMMENT 'Timezone',
35
  `individual_sign` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Individual Sign',
36
  `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Created At',
37
  `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT 'Updated At',
38
  PRIMARY KEY (`guid`),
39
  CONSTRAINT `user_profile_fk` FOREIGN KEY (`guid`) REFERENCES `user` (`guid`) ON DELETE CASCADE ON UPDATE CASCADE
40
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Profile';
41
```
42
 *
43
 * @property string $nickname Nickname
44
 * @property string $first_name First Name
45
 * @property string $last_name Last Name
46
 * @property string $gender Gender
47
 * @property string $gravatar_type Gravatar Type
48
 * @property string $gravatar Gravatar
49
 * @property string $timezone Timezone
50
 * @property string $individual_sign Individual Signature
51
 *
52
 * @property-read User $user
53
 *
54
 * @version 1.0
55
 * @author vistart <[email protected]>
56
 */
57
class Profile extends BaseBlameableModel
58
{
59
    public $createdByAttribute = 'guid';
60
61
    // The host of Profile is only permitted to modify it.
62
    public $updatedByAttribute = false;
63
64
    // Profile do not have its identifier.
65
    public $idAttribute = false;
66
67
    // Profile do not need to record IP address.
68
    public $enableIP = 0;
69
70
    /**
71
     * @var string Specify the nickname as the content attribute.
72
     */
73
    public $contentAttribute = 'nickname';
74
75
    const SCENARIO_UPDATE = 'update';
76
77
    public function attributeLabels()
78
    {
79
        return [
80
            $this->contentAttribute => Yii::t('user', 'Nickname'),
81
            'first_name' => Yii::t('user', 'First Name'),
82
            'last_name' => Yii::t('user', 'Last Name'),
83
            'gender' => Yii::t('user', 'Gender'),
84
            'gravatar_type' => Yii::t('user', 'Gravatar Type'),
85
            'gravatar' => Yii::t('user', 'Gravatar'),
86
            'timezone' => Yii::t('user', 'Timezone'),
87
            'individual_sign' => Yii::t('user', 'Individual Signature'),
88
            $this->createdByAttribute => Yii::t('user', 'Created By'),
89
            $this->createdAtAttribute => Yii::t('user', 'Creation Time'),
90
            $this->updatedAtAttribute => Yii::t('user', 'Last Updated Time'),
91
        ];
92
    }
93
94
    /**
95
     * Get rules associated with individual sign attribute.
96
     * You can override this method if current rules do not satisfy your needs.
97
     * If you do not use individual sign attribute, please override this method and return empty array.
98 16
     * @return array Rules associated with individual sign.
99
     */
100
    public function getIndividualSignRules()
101 16
    {
102
        return [
103
            ['individual_sign', 'string', 'skipOnEmpty' => true],
104
            ['individual_sign', 'default', 'value' => ''],
105
        ];
106
    }
107
108
    /**
109
     * Get rules associated with name attribute.
110
     * You can override this method if current rules do not satisfy your needs.
111
     * If you do not use name attribute, please override this method and return empty array.
112 16
     * @return array Rules associated with name.
113
     */
114
    public function getNameRules()
115 16
    {
116
        return [
117
            [['first_name', 'last_name'], 'string', 'max' => 255, 'skipOnEmpty' => true],
118
            [['first_name', 'last_name'], 'default', 'value' => ''],
119
        ];
120
    }
121
122
    const GENDER_UNSPECIFIED = 0;
123
    const GENDER_MALE = 1;
124
    const GENDER_FEMALE = 2;
125
126
    public static $genders = [
127
        self::GENDER_UNSPECIFIED => 'Unspecified',
128
        self::GENDER_MALE => 'Male',
129
        self::GENDER_FEMALE => 'Female',
130
    ];
131
132
    /**
133
     * Get rules associated with gender attribute.
134
     * You can override this method if current rules do not satisfy your needs.
135
     * If you do not use gender attribute, please override this method and return empty array.
136 16
     * @return array Rules associated with gender.
137
     */
138
    public function getGenderRules()
139 16
    {
140 16
        return [
141
            ['gender', 'default', 'value' => 0],
142
            ['gender', 'in', 'range' => array_keys(static::$genders)],
143
        ];
144
    }
145
146
    public static function getGenderDesc($gender = null)
147
    {
148
        if (array_key_exists($gender, self::$genders)) {
149
            return Yii::t('user', self::$genders[$gender]);
150
        }
151
        return null;
152
    }
153
154
    public static function getGenderDescs()
155
    {
156
        $genders = [];
157
        foreach (self::$genders as $key => $gender) {
158
            $genders[$key] = static::getGenderDesc($key);
159
        }
160
        return $genders;
161
    }
162
163
    public static function getGenderDescsWithEmpty()
164
    {
165
        return array_merge(['' => Yii::t('user', 'All')], static::getGenderDescs());
166 16
    }
167
168
    public function getGravatarRules()
169 16
    {
170
        return [
171
            ['gravatar_type', 'default', 'value' => 0],
172
            ['gravatar_type', 'integer'],
173
            ['gravatar', 'default', 'value' => ''],
174
            ['gravatar', 'string', 'max' => 255],
175
        ];
176 16
    }
177
178
    public function getTimezoneRules()
179 16
    {
180 16
        return [
181
            ['timezone', 'in', 'range' => \DateTimeZone::listIdentifiers()],
182
            ['timezone', 'default', 'value' => Yii::$app->timeZone],
183
        ];
184
    }
185
186
    /**
187 16
     * @inheritdoc
188
     */
189 16
    public function rules()
190 16
    {
191 16
        return array_merge($this->getNameRules(),
192 16
                $this->getGenderRules(),
193 16
                $this->getGravatarRules(),
194 16
                $this->getTimezoneRules(),
195
                $this->getIndividualSignRules(),
196
                parent::rules());
197
    }
198
199
    /**
200 18
     * @inheritdoc
201
     */
202 18
    public static function tableName()
203
    {
204
        return '{{%profile}}';
205 16
    }
206
207 16
    public function scenarios()
208 16
    {
209
        return array_merge(parent::scenarios(), [
210
            self::SCENARIO_UPDATE => [$this->contentAttribute, 'first_name', 'last_name', 'gender', 'gravatar_type', 'gravatar', 'timezone', 'individual_sign'],
211
        ]);
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217
    public function init()
218
    {
219
        $this->on(static::EVENT_AFTER_UPDATE, [$this, 'onInvalidTags']);
220
        $this->on(static::EVENT_AFTER_DELETE, [$this, 'onInvalidTags']);
221
        parent::init();
222
    }
223
224
    /**
225
     * @var string
226
     */
227
    public $cacheTagPrefix = 'tag_user_profile_';
228
229
    /**
230
     * @return string
231
     */
232
    public function getCacheTag()
233
    {
234
        return $this->cacheTagPrefix . $this->getID();
235
    }
236
237
    /**
238
     * @param Event $event
239
     */
240
    public function onInvalidTags($event)
241
    {
242
        $sender = $event->sender;
243
        /*@var $sender static */
244
        TagDependency::invalidate(Yii::$app->cache, $sender->getCacheTag());
245
    }
246
}
247