Completed
Push — master ( 5df893...523fca )
by Дмитрий
04:24
created

Vk::getRequestTokenUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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