Completed
Push — master ( a178a2...762452 )
by Дмитрий
03:58
created

Vk::getIdentity()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.551
c 0
b 0
f 0
cc 5
eloc 28
nc 5
nop 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
    public function getBaseUri()
32
    {
33
        return 'https://api.vk.com/';
34
    }
35
36
    public function getAuthorizeUri()
37
    {
38
        return 'https://oauth.vk.com/authorize';
39
    }
40
41
    public function getRequestTokenUri()
42
    {
43
        return 'https://oauth.vk.com/access_token';
44
    }
45
46
    public function getName()
47
    {
48
        return 'vk';
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function parseToken($body)
55
    {
56
        $result = json_decode($body, true);
57
        if (!$result) {
58
            throw new InvalidAccessToken;
59
        }
60
61
        if (isset($result['email'])) {
62
            $this->email = $result['email'];
63
        }
64
65
        return new AccessToken($result);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getIdentity(AccessTokenInterface $accessToken)
72
    {
73
        $response = $this->httpClient->request(
74
            $this->getBaseUri() . 'method/users.get',
75
            [
76
                'v' => '5.24',
77
                'access_token' => $accessToken->getToken(),
78
                'fields' => $this->getFieldsInline()
79
            ]
80
        );
81
82
        if (!$response->isSuccess()) {
83
            throw new InvalidResponse(
84
                'API response with error code',
85
                $response
86
            );
87
        }
88
89
        $result = $response->json();
90
        if (!$result) {
91
            throw new InvalidResponse(
92
                'API response is not a valid JSON object',
93
                $response->getBody()
94
            );
95
        }
96
97
        $hydrator = new ObjectMap(
98
            [
99
                'id' => 'id',
100
                'first_name' => 'firstname',
101
                'last_name' => 'lastname',
102
                'email' => 'email',
103
                'bdate' => 'birthday',
104
                'nickname' => 'username',
105
                'sex' => 'sex',
106
            ]
107
        );
108
109
        $user = $hydrator->hydrate(new User(), $result->response[0]);
110
111
        if ($user->sex) {
112
            $user->sex = $user->sex === 1 ? 'female' : 'male';
113
        }
114
115
        $user->email = $this->email;
116
117
        return $user;
118
    }
119
}
120