Passed
Push — master ( bbe65f...8e1098 )
by AD
02:02
created

Yandex   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Test Coverage

Coverage 82.61%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 76
ccs 19
cts 23
cp 0.8261
rs 10
c 1
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthorizeUri() 0 3 1
A getRequestTokenUri() 0 3 1
A getName() 0 3 1
A getBaseUri() 0 3 1
A prepareRequest() 0 6 2
A getIdentity() 0 25 2
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 Yandex extends \SocialConnect\OAuth2\AbstractProvider
15
{
16
    const NAME = 'yandex';
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 4
    public function getBaseUri()
22
    {
23 4
        return 'https://login.yandex.ru/';
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 2
    public function getAuthorizeUri()
30
    {
31 2
        return 'https://oauth.yandex.ru/authorize';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function getRequestTokenUri()
38
    {
39 2
        return 'https://oauth.yandex.ru/token';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getName()
46
    {
47 3
        return self::NAME;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void
54
    {
55 3
        $query['format'] = 'json';
56
57 3
        if ($accessToken) {
58 3
            $headers['Authorization'] = "OAuth {$accessToken->getToken()}";
59
        }
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 3
    public function getIdentity(AccessTokenInterface $accessToken)
66
    {
67 3
        $result = $this->request('GET', 'info', [], $accessToken);
68
69 1
        $hydrator = new ArrayHydrator([
70
            'id' => 'id',
71
            'first_name' => 'firstname',
72
            'last_name' => 'lastname',
73
            'default_email' => 'email',
74
            'real_name' => 'fullname',
75 1
            'sex' => static function ($value, User $user) {
76
                $user->setSex($value === 'male' ?  User::SEX_MALE : User::SEX_FEMALE);
77
            },
78 1
            'birthday' => static function ($value, User $user) {
79
                $user->setBirthday(
80
                    new \DateTime($value)
81
                );
82
            },
83
            'login' => 'username',
84 1
            'default_avatar_id' => static function ($value, User $user) {
85
                $user->pictureURL = 'https://avatars.yandex.net/get-yapic/'.$value.'/islands-200';
86
            }
87
        ]);
88
89 1
        return $hydrator->hydrate(new User(), $result);
90
    }
91
}
92