Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
created

UserProvider::getTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
3
namespace app\models;
4
5
use yii\helpers\ArrayHelper;
6
use app\models\User;
7
8
/**
9
 * This is the model class for table "user_provider"
10
 *
11
 * @property integer $id
12
 * @property integer $user_id
13
 * @property integer $type
14
 * @property string $profile_id
15
 * @property string $profile_url
16
 * @property string $access_token
17
 * @property string $access_token_secret
18
 */
19
class UserProvider extends \yii\db\ActiveRecord
20
{
21
    const TYPE_TWITTER   = 1;
22
    const TYPE_FACEBOOK  = 2;
23
    const TYPE_VKONTAKTE = 3;
24
25
    /**
26
     * @inheritdoc
27
     */
28 33
    public static function tableName()
29
    {
30 33
        return 'user_provider';
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function rules()
37
    {
38
        return [
39
            [
40
                ['type', 'profile_id', 'profile_url', 'access_token', 'access_token_secret'], 'safe'
41
            ],
42
        ];
43
    }
44
45
    /**
46
     * @return \yii\db\ActiveQuery
47
     */
48
    public function getUser()
49
    {
50
        return $this->hasOne(User::className(), ['id' => 'user_id']);
51
    }
52
53
    /**
54
     * Get types
55
     *
56
     * @return array
57
     */
58 11
    public static function getTypes()
59
    {
60
        return [
61 11
            self::TYPE_TWITTER => 'twitter',
62 11
            self::TYPE_FACEBOOK => 'facebook',
63 11
            self::TYPE_VKONTAKTE => 'vkontakte',
64
        ];
65
    }
66
67
    /**
68
     * Get type by id
69
     *
70
     * @return string $name
71
     * @return int
72
     */
73 9
    public static function getTypeByName($name)
74
    {
75 9
        $types = array_flip(self::getTypes());
76 9
        return isset($types[$name]) ? $types[$name] : false;
77
    }
78
79
    /**
80
     * Get type name
81
     *
82
     * @return string
83
     */
84 1
    public function getTypeName()
85
    {
86 1
        $types = self::getTypes();
87 1
        return isset($types[$this->type]) ? $types[$this->type] : false;
88
    }
89
90
    /**
91
     * Finds by type of provider
92
     *
93
     * @param int $type
94
     * @param int $profileId
95
     * @return app\models\UserProvider|null
96
     */
97 8
    public static function findByProvider($type, $profileId)
98
    {
99 8
        return static::findOne(['type' => $type, 'profile_id' => $profileId]);
100
    }
101
}
102