Completed
Push — master ( d3f389...b7db91 )
by Дмитрий
03:39
created

Provider::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\Vk;
8
9
use SocialConnect\Auth\Exception\InvalidAccessToken;
10
use SocialConnect\Auth\Exception\InvalidResponse;
11
use SocialConnect\Auth\Provider\OAuth2\AccessToken;
12
use SocialConnect\Common\Entity\User;
13
use SocialConnect\Common\Http\Client\Client;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Provider extends \SocialConnect\Auth\Provider\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $requestHttpMethod = Client::GET;
22
23 1
    public function getBaseUri()
24
    {
25 1
        return 'https://api.vk.com/';
26
    }
27
28
    public function getAuthorizeUri()
29
    {
30
        return 'https://oauth.vk.com/authorize';
31
    }
32
33
    public function getRequestTokenUri()
34
    {
35
        return 'https://oauth.vk.com/access_token';
36
    }
37
38
    public function getName()
39
    {
40
        return 'vk';
41
    }
42
43
    /**
44
     * @param string $body
45
     * @return AccessToken
46
     * @throws InvalidAccessToken
47
     */
48 3
    public function parseToken($body)
49
    {
50 3
        $result = json_decode($body);
51 3
        if (!$result) {
52 2
            throw new InvalidAccessToken;
53
        }
54
55 1
        if (!isset($result->access_token) || empty($result->access_token)) {
56
            throw new InvalidAccessToken;
57
        }
58
59 1
        return new AccessToken($result->access_token);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function getIdentity(AccessToken $accessToken)
66
    {
67 1
        $response = $this->service->getHttpClient()->request(
68 1
            $this->getBaseUri() . 'method/users.get',
69
            [
70 1
                'v' => '5.24',
71 1
                'access_token' => $accessToken->getToken(),
72 1
                'fields' => $this->getFieldsInline()
73 1
            ]
74 1
        );
75
76 1
        if (!$response->isSuccess()) {
77
            throw new InvalidResponse(
78
                'API response with error code',
79
                $response
80
            );
81
        }
82
83 1
        $result = $response->json();
84 1
        if (!$result) {
85
            throw new InvalidResponse(
86
                'API response is not a valid JSON object',
87
                $response->getBody()
88
            );
89
        }
90
91 1
        $hydrator = new ObjectMap(array(
92 1
            'id' => 'id',
93 1
            'first_name' => 'firstname',
94 1
            'last_name' => 'lastname',
95 1
            'email' => 'email',
96 1
            'bdate' => 'birthday',
97 1
            'nickname' => 'username',
98 1
            'sex' => 'sex',
99 1
        ));
100
101 1
        $user = $hydrator->hydrate(new User(), $result->response[0]);
102
103 1
        if ($user->sex) {
104
            $user->sex = $user->sex === 1 ? 'female' : 'male';
105
        }
106
107 1
        return $user;
108
    }
109
}
110