SocialNetworkAccount::getIsConnected()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Model;
13
14
use Da\User\Query\SocialNetworkAccountQuery;
15
use Da\User\Traits\ContainerAwareTrait;
16
use Da\User\Traits\ModuleAwareTrait;
17
use Yii;
18
use yii\base\Exception;
19
use yii\base\InvalidParamException;
20
use yii\db\ActiveRecord;
21
use yii\helpers\Url;
22
23
/**
24
 * /**
25
 * @property int $id          Id
26
 * @property int $user_id     User id, null if account is not bind to user
27
 * @property string $provider     Name of service
28
 * @property string $client_id    Account id
29
 * @property string $data         Account properties returned by social network (json encoded)
30
 * @property string $decodedData  Json-decoded properties
31
 * @property string $code
32
 * @property string $email
33
 * @property string $username
34
 * @property int $created_at
35
 * @property User $user        User that this account is connected for
36
 */
37
class SocialNetworkAccount extends ActiveRecord
38
{
39
    use ModuleAwareTrait;
40
    use ContainerAwareTrait;
41
42
    /**
43
     * @var array json decoded properties
44
     */
45
    protected $decodedData;
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public static function tableName()
51
    {
52 1
        return '{{%social_account}}';
53
    }
54
55
    /**
56
     * @return bool Whether this social account is connected to user
57
     */
58
    public function getIsConnected()
59
    {
60
        return null !== $this->user_id;
61
    }
62
63
    /**
64
     * @return array json decoded properties
65
     */
66
    public function getDecodedData()
67
    {
68
        if ($this->data !== null && $this->decodedData === null) {
69
            $this->decodedData = json_decode($this->data);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($this->data) of type * is incompatible with the declared type array of property $decodedData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        }
71
72
        return $this->decodedData;
73
    }
74
75
    /**
76
     * @throws Exception
77
     * @throws InvalidParamException
78
     * @return string                the connection url
79
     */
80
    public function getConnectionUrl()
81
    {
82
        $code = Yii::$app->security->generateRandomString();
83
        $this->updateAttributes(['code' => md5($code)]);
84
85
        return Url::to(['/user/registration/connect', 'code' => $code]);
86
    }
87
88
    /**
89
     * Connects account to a user.
90
     *
91
     * @param User $user
92
     *
93
     * @return int
94
     */
95
    public function connect(User $user)
96
    {
97
        return $this->updateAttributes(
98
            [
99
                'username' => null,
100
                'email' => null,
101
                'code' => null,
102
                'user_id' => $user->id,
103
            ]
104
        );
105
    }
106
107
    /**
108
     * @return \yii\db\ActiveQuery
109
     */
110
    public function getUser()
111
    {
112
        return $this->hasOne($this->getClassMap()->get(User::class), ['id' => 'user_id']);
113
    }
114
115
    /**
116
     * @return SocialNetworkAccountQuery
117
     */
118 15
    public static function find()
119
    {
120 15
        return new SocialNetworkAccountQuery(static::class);
121
    }
122
}
123