Completed
Push — master ( d2cebe...dfbba4 )
by Дмитрий
12:05
created

Vk::getBaseUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $requestHttpMethod = Client::GET;
23
24
    /**
25
     * Vk returns email inside AccessToken
26
     *
27
     * @var string|null
28
     */
29
    protected $email;
30
31 5
    public function getBaseUri()
32
    {
33 5
        return 'https://api.vk.com/';
34
    }
35
36 2
    public function getAuthorizeUri()
37
    {
38 2
        return 'https://oauth.vk.com/authorize';
39
    }
40
41 3
    public function getRequestTokenUri()
42
    {
43 3
        return 'https://oauth.vk.com/access_token';
44
    }
45
46 4
    public function getName()
47
    {
48 4
        return 'vk';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 3
    public function parseToken($body)
55
    {
56 3
        $result = json_decode($body, true);
57 3
        if (!$result) {
58 2
            throw new InvalidAccessToken;
59
        }
60
61 1
        if (isset($result['email'])) {
62
            $this->email = $result['email'];
63
        }
64
65 1
        return new AccessToken($result);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function getIdentity(AccessTokenInterface $accessToken)
72
    {
73 4
        $response = $this->httpClient->request(
74 4
            $this->getBaseUri() . 'method/users.get',
75
            [
76 4
                'v' => '5.24',
77 4
                'access_token' => $accessToken->getToken(),
78 4
                'fields' => $this->getFieldsInline()
79 4
            ]
80 4
        );
81
82 4
        if (!$response->isSuccess()) {
83 1
            throw new InvalidResponse(
84 1
                'API response with error code',
85
                $response
86 1
            );
87
        }
88
89 3
        $result = $response->json();
90 3
        if (!$result) {
91 2
            throw new InvalidResponse(
92 2
                'API response is not a valid JSON object',
93 2
                $response->getBody()
94 2
            );
95
        }
96
97 1
        $hydrator = new ObjectMap(
98
            [
99 1
                'id' => 'id',
100 1
                'first_name' => 'firstname',
101 1
                'last_name' => 'lastname',
102 1
                'email' => 'email',
103 1
                'bdate' => 'birthday',
104 1
                'nickname' => 'username',
105 1
                'sex' => 'sex',
106
            ]
107 1
        );
108
109
        /** @var User $user */
110 1
        $user = $hydrator->hydrate(new User(), $result->response[0]);
111
112 1
        if ($user->sex) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $user->sex of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113 1
            $user->sex = $user->sex === 1 ? 'female' : 'male';
1 ignored issue
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $user->sex (string) and 1 (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
114 1
        }
115
116 1
        $user->email = $this->email;
117 1
        $user->emailVerified = true;
118
119 1
        return $user;
120
    }
121
}
122