Completed
Push — master ( 20a8a3...23113a )
by Дмитрий
03:35
created

Vk::getRequestTokenUri()   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 3
    public function getBaseUri()
32
    {
33 3
        return 'https://api.vk.com/';
34
    }
35
36 1
    public function getAuthorizeUri()
37
    {
38 1
        return 'https://oauth.vk.com/authorize';
39
    }
40
41 2
    public function getRequestTokenUri()
42
    {
43 2
        return 'https://oauth.vk.com/access_token';
44
    }
45
46 3
    public function getName()
47
    {
48 3
        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 3
    public function getIdentity(AccessTokenInterface $accessToken)
72
    {
73 3
        $response = $this->httpClient->request(
74 3
            $this->getBaseUri() . 'method/users.get',
75
            [
76 3
                'v' => '5.24',
77 3
                'access_token' => $accessToken->getToken(),
78 3
                'fields' => $this->getFieldsInline()
79 3
            ]
80 3
        );
81
82 3
        if (!$response->isSuccess()) {
83 1
            throw new InvalidResponse(
84 1
                'API response with error code',
85
                $response
86 1
            );
87
        }
88
89 2
        $result = $response->json();
90 2
        if (!$result) {
91 1
            throw new InvalidResponse(
92 1
                'API response is not a valid JSON object',
93 1
                $response->getBody()
94 1
            );
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 1
        $user = $hydrator->hydrate(new User(), $result->response[0]);
110
111 1
        if ($user->sex) {
112 1
            $user->sex = $user->sex === 1 ? 'female' : 'male';
113 1
        }
114
115 1
        $user->email = $this->email;
116
117 1
        return $user;
118
    }
119
}
120