Completed
Push — master ( a573af...4c1263 )
by Дмитрий
01:54
created

Google::getIdentity()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.054

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 17
cts 20
cp 0.85
rs 8.9381
c 0
b 0
f 0
cc 4
nc 6
nop 1
crap 4.054

How to fix   Long Method   

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
/**
4
 * SocialConnect project
5
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
6
 * @author Alexander Fedyashov <[email protected]>
7
 */
8
9
namespace SocialConnect\OAuth2\Provider;
10
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Provider\Exception\InvalidResponse;
14
use SocialConnect\OAuth2\AbstractProvider;
15
use SocialConnect\OAuth2\AccessToken;
16
use SocialConnect\Common\Entity\User;
17
use SocialConnect\Common\Hydrator\ObjectMap;
18
19
/**
20
 * Class Provider
21
 * @package SocialConnect\Google
22
 */
23
class Google extends AbstractProvider
24
{
25
    const NAME = 'google';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    public function getBaseUri()
31
    {
32 3
        return 'https://www.googleapis.com/';
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function getAuthorizeUri()
39
    {
40 2
        return 'https://accounts.google.com/o/oauth2/auth';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 2
    public function getRequestTokenUri()
47
    {
48 2
        return 'https://accounts.google.com/o/oauth2/token';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function getName()
55
    {
56 3
        return self::NAME;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 3
    public function parseToken($body)
63
    {
64 3
        $result = json_decode($body, true);
65 3
        if ($result) {
66 1
            return new AccessToken($result);
67
        }
68
69 2
        throw new InvalidAccessToken('Provider response with not valid JSON');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public function getIdentity(AccessTokenInterface $accessToken)
76
    {
77 2
        $response = $this->httpClient->request(
78 2
            $this->getBaseUri() . 'oauth2/v1/userinfo',
79
            [
80 2
                'access_token' => $accessToken->getToken()
81
            ]
82
        );
83
84 2
        $fields = $this->getArrayOption('identity.fields', [
85 2
            'id',
86
            'email',
87
            'verified_email',
88
            'name',
89
            'given_name',
90
            'family_name',
91
            'picture',
92
            'locale',
93
            //
94
            'gender',
95
            'hd',
96
            'link',
97
        ]);
98 2
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99 2
            $parameters['fields'] = implode(',', $fields);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
100
        }
101
102 2
        if (!$response->isSuccess()) {
103 1
            throw new InvalidResponse(
104 1
                'API response with error code',
105 1
                $response
106
            );
107
        }
108
109 1
        $result = $response->json();
110 1
        if (!$result) {
111 1
            throw new InvalidResponse(
112 1
                'API response is not a valid JSON object',
113 1
                $response
114
            );
115
        }
116
117
        $hydrator = new ObjectMap(
118
            [
119
                'id' => 'id',
120
                'given_name' => 'firstname',
121
                'family_name' => 'lastname',
122
                'email' => 'email',
123
                'verified_email' => 'emailVerified',
124
                'name' => 'fullname',
125
                'gender' => 'sex',
126
                'picture' => 'pictureURL'
127
            ]
128
        );
129
130
        return $hydrator->hydrate(new User(), $result);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function getScopeInline()
137
    {
138
        return implode(' ', $this->scope);
139
    }
140
}
141