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

Vk   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 30
c 5
b 1
f 0
dl 0
loc 79
ccs 30
cts 33
cp 0.9091
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRequestTokenUri() 0 3 1
A getIdentity() 0 37 3
A getName() 0 3 1
A prepareRequest() 0 4 2
A getBaseUri() 0 3 1
A getAuthorizeUri() 0 3 1
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