Completed
Branch master (bda8d5)
by Дмитрий
02:19
created

Vk::getIdentity()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.002

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 56
rs 8.7697
c 2
b 0
f 0
ccs 25
cts 26
cp 0.9615
cc 6
nc 10
nop 1
crap 6.002

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
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Http\Client\Client;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
17
class Vk extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    const NAME = 'vk';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected $requestHttpMethod = Client::GET;
25
26
    /**
27
     * Vk returns email inside AccessToken
28
     *
29
     * @var string|null
30
     */
31
    protected $email;
32
33 5
    public function getBaseUri()
34
    {
35 5
        return 'https://api.vk.com/';
36
    }
37
38 2
    public function getAuthorizeUri()
39
    {
40 2
        return 'https://oauth.vk.com/authorize';
41
    }
42
43 3
    public function getRequestTokenUri()
44
    {
45 3
        return 'https://oauth.vk.com/access_token';
46
    }
47
48 4
    public function getName()
49
    {
50 4
        return self::NAME;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function parseToken($body)
57
    {
58 3
        $result = json_decode($body, true);
59 3
        if (!$result) {
60 2
            throw new InvalidAccessToken;
61
        }
62
63 1
        if (isset($result['email'])) {
64
            $this->email = $result['email'];
65
        }
66
67 1
        return new AccessToken($result);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 4
    public function getIdentity(AccessTokenInterface $accessToken)
74
    {
75
        $parameters = [
76 4
            'v' => '5.100',
77 4
            'access_token' => $accessToken->getToken(),
78
        ];
79
80 4
        $fields = $this->getArrayOption('identity.fields', []);
81 4
        if ($fields) {
82
            $parameters['fields'] = implode(',', $fields);
83
        }
84
85 4
        $response = $this->httpClient->request(
86 4
            $this->getBaseUri() . 'method/users.get',
87 4
            $parameters
88
        );
89
90 4
        if (!$response->isSuccess()) {
91 1
            throw new InvalidResponse(
92 1
                'API response with error code',
93 1
                $response
94
            );
95
        }
96
97 3
        $result = $response->json();
98 3
        if (!$result) {
99 2
            throw new InvalidResponse(
100 2
                'API response is not a valid JSON object',
101 2
                $response
102
            );
103
        }
104
105 1
        $hydrator = new ObjectMap(
106
            [
107 1
                'id' => 'id',
108
                'first_name' => 'firstname',
109
                'last_name' => 'lastname',
110
                'email' => 'email',
111
                'bdate' => 'birthday',
112
                'screen_name' => 'username',
113
                'sex' => 'sex',
114
                'photo_max_orig' => 'pictureURL',
115
            ]
116
        );
117
118
        /** @var User $user */
119 1
        $user = $hydrator->hydrate(new User(), $result->response[0]);
120
121 1
        if ($user->sex) {
122 1
            $user->sex = $user->sex === 1 ? 'female' : 'male';
123
        }
124
125 1
        $user->email = $this->email;
126 1
        $user->emailVerified = true;
127
128 1
        return $user;
129
    }
130
}
131