Completed
Push — master ( fec932...b6246a )
by Дмитрий
03:25 queued 01:17
created

Vk::getIdentity()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3.0261

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 3
eloc 21
c 4
b 1
f 0
nc 2
nop 1
dl 0
loc 37
ccs 18
cts 21
cp 0.8571
crap 3.0261
rs 9.584
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
declare(strict_types=1);
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\ArrayHydrator;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Common\Entity\User;
13
14
class Vk extends \SocialConnect\OAuth2\AbstractProvider
15
{
16
    const NAME = 'vk';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $requestHttpMethod = 'GET';
22
23 4
    public function getBaseUri()
24
    {
25 4
        return 'https://api.vk.com/';
26
    }
27
28 2
    public function getAuthorizeUri()
29
    {
30 2
        return 'https://api.vk.com/oauth/authorize';
31
    }
32
33 2
    public function getRequestTokenUri()
34
    {
35 2
        return 'https://api.vk.com/oauth/token';
36
    }
37
38 3
    public function getName()
39
    {
40 3
        return self::NAME;
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
47
    {
48 3
        if ($accessToken) {
49 3
            $query['access_token'] = $accessToken->getToken();
50
        }
51 3
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function getIdentity(AccessTokenInterface $accessToken)
57
    {
58
        $query = [
59 3
            'v' => '5.100'
60
        ];
61
62 3
        $fields = $this->getArrayOption('identity.fields', []);
63 3
        if ($fields) {
64
            $query['fields'] = implode(',', $fields);
65
        }
66
67 3
        $response = $this->request('GET', 'method/users.get', $query, $accessToken);
68
69 1
        $hydrator = new ArrayHydrator([
70 1
            'id' => 'id',
71 1
            'first_name' => 'firstname',
72 1
            'last_name' => 'lastname',
73
            'bdate' => static function ($value, User $user) {
74
                $user->setBirthday(
75
                    new \DateTime($value)
76
                );
77 1
            },
78
            'sex' => static function ($value, User $user) {
79 1
                $user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE);
80 1
            },
81 1
            'screen_name' => 'username',
82 1
            'photo_max_orig' => 'pictureURL',
83
        ]);
84
85
        /** @var User $user */
86 1
        $user = $hydrator->hydrate(new User(), $response['response'][0]);
87
88
        // Vk returns email inside AccessToken
89 1
        $user->email = $accessToken->getEmail();
90 1
        $user->emailVerified = true;
91
92 1
        return $user;
93
    }
94
}
95