Completed
Push — master ( acca1c...3f7868 )
by Дмитрий
03:40
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\Auth\Provider;
8
9
use SocialConnect\Auth\AccessTokenInterface;
10
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Auth\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 1
    public function getBaseUri()
32
    {
33 1
        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 3
    public function parseToken($body)
55
    {
56 3
        $result = json_decode($body);
57 3
        if (!$result) {
58 2
            throw new InvalidAccessToken;
59
        }
60
61 1
        if (!isset($result->access_token) || empty($result->access_token)) {
62
            throw new InvalidAccessToken;
63
        }
64
65 1
        if (isset($result->email)) {
66
            $this->email = $result->email;
67
        }
68
69 1
        $token = new AccessToken($result->access_token);
70 1
        $token->setUid($result->user_id);
71
72 1
        return $token;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function getIdentity(AccessTokenInterface $accessToken)
79
    {
80 1
        $response = $this->service->getHttpClient()->request(
81 1
            $this->getBaseUri() . 'method/users.get',
82
            [
83 1
                'v' => '5.24',
84 1
                'access_token' => $accessToken->getToken(),
85 1
                'fields' => $this->getFieldsInline()
86 1
            ]
87 1
        );
88
89 1
        if (!$response->isSuccess()) {
90
            throw new InvalidResponse(
91
                'API response with error code',
92
                $response
93
            );
94
        }
95
96 1
        $result = $response->json();
97 1
        if (!$result) {
98
            throw new InvalidResponse(
99
                'API response is not a valid JSON object',
100
                $response->getBody()
101
            );
102
        }
103
104 1
        $hydrator = new ObjectMap(array(
105 1
            'id' => 'id',
106 1
            'first_name' => 'firstname',
107 1
            'last_name' => 'lastname',
108 1
            'email' => 'email',
109 1
            'bdate' => 'birthday',
110 1
            'nickname' => 'username',
111 1
            'sex' => 'sex',
112 1
        ));
113
114 1
        $user = $hydrator->hydrate(new User(), $result->response[0]);
115
116 1
        if ($user->sex) {
117
            $user->sex = $user->sex === 1 ? 'female' : 'male';
118
        }
119
120 1
        $user->email = $this->email;
121
122 1
        return $user;
123
    }
124
}
125