GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AuthClientHelper::mapUserAttributesWithService()   C
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 6.526
c 0
b 0
f 0
cc 10
nc 11
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\components;
4
5
use app;
6
use app\modules\user\models\UserService;
7
use yii\base\ErrorException;
8
9
/**
10
 * AuthClientHelper is a helper class for serving login through social networks and retrieving needed information by api
11
 * @package app\components
12
 */
13
class AuthClientHelper
14
{
15
    public static $ServiceIdMapping = [
16
        'app\modules\user\authclients\GitHub' => 'id',
17
        'yii\authclient\clients\YandexOpenId' => 'id',
18
        'yii\authclient\clients\Twitter' => 'id',
19
        'app\modules\user\authclients\Facebook' => 'id',
20
        'app\modules\user\authclients\VKontakte' => 'uid',
21
        'yii\authclient\clients\YandexOAuth' => 'id',
22
        'yii\authclient\clients\GoogleOAuth' => 'id',
23
        'app\modules\user\authclients\PayPal' => 'user_id'
24
    ];
25
26
    /**
27
     * Finds service record for current logged client and returns corresponding user.
28
     * @param \yii\authclient\BaseClient $client AuthClient instance with social authenticated details(ie. user attributes)
29
     * @throws ErrorException
30
     * @return app\modules\user\models\User|null
31
     */
32
    public static function findUserByService(\yii\authclient\BaseClient $client)
33
    {
34
        $serviceType = $client->className();
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
        if (isset(static::$ServiceIdMapping[$client->className()])) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
36
            $id_attribute = static::$ServiceIdMapping[$client->className()];
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
37
            $attributes = $client->getUserAttributes();
38
            $serviceId = null;
0 ignored issues
show
Unused Code introduced by
$serviceId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
            if (isset($attributes[$id_attribute])) {
40
                $serviceId = $attributes[$id_attribute];
41
            } else {
42
                throw new ErrorException("No user identified supplied by social service.");
43
            }
44
            /** @var \app\modules\user\models\UserService $service */
45
            $service = UserService::find()
46
                ->where([
47
                    'service_type' => $serviceType,
48
                    'service_id' => $serviceId,
49
                ])
50
                ->with('user')
51
                ->one();
52
53
            if ($service === null) {
54
                return null;
55
            }
56
57
            return $service->user;
58
        } else {
59
            throw new ErrorException("Unidentified social service used.");
60
        }
61
62
    }
63
64
    /**
65
     * Retrieves additional profile information which can be needed for first-login(registration)
66
     * and which was not provided by first api call.
67
     * Returns merged user attributes
68
     * @param \yii\authclient\BaseClient $client
69
     * @return \yii\authclient\BaseClient Client with merged attributes
70
     */
71
    public static function retrieveAdditionalData(\yii\authclient\BaseClient $client)
72
    {
73
        $attributes = $client->getUserAttributes();
74
75
        switch ($client->className()) {
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76
            case 'app\modules\user\authclients\GitHub':
77
                try {
78
                    /** @var \app\modules\user\authclients\GitHub $client */
79
                    $emails = $client->api('user/emails');
80
81
                    foreach ($emails as $email) {
82
                        if ($email['primary'] === true) {
83
                            $attributes['email'] = $email['email'];
84
                            break;
85
                        }
86
                    }
87
88
                } catch (\yii\authclient\InvalidResponseException $e) {
89
                    // no email :-(
90
                }
91
                break;
92
            default:
93
                break;
94
        }
95
        $client->setUserAttributes($attributes);
96
        return $client;
97
    }
98
99
100
    /**
101
     * Converts service attributes to app\modules\user\models\User model attributes
102
     * @param \yii\authclient\BaseClient $client
103
     * @return array Array of attributes by model type which we can apply by $model->setAttributes()
104
     */
105
    public static function mapUserAttributesWithService(\yii\authclient\BaseClient $client)
106
    {
107
        $mappings = [
108
            'service' => [
109
                // id of user in service
110
                'service_id' => static::$ServiceIdMapping,
111
            ],
112
            'user' => [
113
                'username' => [
114
                    'app\modules\user\authclients\GitHub' => 'login',
115
                    'yii\authclient\clients\Twitter' => 'screen_name',
116
                    'app\modules\user\authclients\VKontakte' => 'nickname',
117
                    'yii\authclient\clients\YandexOAuth' => 'login',
118
                ],
119
                'email' => [
120
                    'app\modules\user\authclients\GitHub' => 'email',
121
                    'yii\authclient\clients\YandexOpenId' => 'email',
122
                    'app\modules\user\authclients\Facebook' => 'email',
123
                    'yii\authclient\clients\YandexOAuth' => 'default_email',
124
                ],
125
                'first_name' => [
126
                    'app\modules\user\authclients\Facebook' => 'first_name',
127
                    'app\modules\user\authclients\VKontakte' => 'first_name',
128
                    'yii\authclient\clients\YandexOAuth' => 'first_name',
129
                ],
130
                'last_name' => [
131
                    'app\modules\user\authclients\Facebook' => 'last_name',
132
                    'app\modules\user\authclients\VKontakte' => 'last_name',
133
                    'yii\authclient\clients\YandexOAuth' => 'last_name',
134
                ],
135
                'avatar_url' => [
136
                    'app\modules\user\authclients\GitHub' => 'avatar_url',
137
                    'yii\authclient\clients\Twitter' => 'profile_image_url',
138
                    'app\modules\user\authclients\VKontakte' => 'photo',
139
                ],
140
                'company' => [
141
                    'app\modules\user\authclients\GitHub' => 'company',
142
                ],
143
                'url' => [
144
                    'app\modules\user\authclients\GitHub' => 'html_url',
145
                ],
146
                'location' => [
147
                    'app\modules\user\authclients\GitHub' => 'location',
148
                ],
149
            ],
150
        ];
151
152
        $class_name = $client->className();
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
153
        $attributes = $client->getUserAttributes();
154
        $result = [];
155
        foreach ($mappings as $model_type => $mappings_by_attribute) {
156
            $result [$model_type] = [];
157
158
            foreach ($mappings_by_attribute as $attribute => $maps) {
159
                if (isset($maps[$class_name])) {
160
                    $key_in_attributes = $maps[$class_name];
161
                    $value = null;
0 ignored issues
show
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
                    if (is_array($key_in_attributes)) {
163
                        $value = [];
164
                        foreach ($key_in_attributes as $key) {
165
                            if (isset($attributes[$key])) {
166
                                $value[] = $attributes[$key];
167
                            }
168
                        }
169
                        if (count($value) > 0) {
170
                            $value = implode(' ', $value);
171
                        } else {
172
                            $value = null;
173
                        }
174
                    } else {
175
                        $value = isset($attributes[$key_in_attributes]) ? $attributes[$key_in_attributes] : null;
176
                    }
177
178
                    if ($value !== null) {
179
                        $result[$model_type][$attribute] = $value;
180
                    }
181
                }
182
            }
183
        }
184
185
        return $result;
186
    }
187
188
}