Completed
Push — master ( 0095fb...15ea44 )
by Дмитрий
04:21
created

Yandex::getIdentity()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.944

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 10
cts 25
cp 0.4
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 1
crap 4.944
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 */
6
7
namespace SocialConnect\OAuth2\Provider;
8
9
use SocialConnect\Provider\AccessTokenInterface;
10
use SocialConnect\Provider\Exception\InvalidAccessToken;
11
use SocialConnect\Provider\Exception\InvalidResponse;
12
use SocialConnect\OAuth2\AccessToken;
13
use SocialConnect\Common\Entity\User;
14
use SocialConnect\Common\Hydrator\ObjectMap;
15
16
class Yandex extends \SocialConnect\OAuth2\AbstractProvider
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 2
    public function getBaseUri()
22
    {
23 2
        return 'https://login.yandex.ru/info';
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 'yandex';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function parseToken($body)
54
    {
55
        if (empty($body)) {
56
            throw new InvalidAccessToken('Provider response with empty body');
57
        }
58
59
        $result = json_decode($body, true);
60
        if ($result) {
61
            return new AccessToken($result);
62
        }
63
64
        throw new InvalidAccessToken('Provider response with not valid JSON');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 1
    public function getIdentity(AccessTokenInterface $accessToken)
71
    {
72 1
        $response = $this->httpClient->request(
73 1
            $this->getBaseUri(),
74
            [
75 1
                'oauth_token' => $accessToken->getToken(),
76
                'format' => 'json'
77 1
            ]
78 1
        );
79
80 1
        if (!$response->isSuccess()) {
81 1
            throw new InvalidResponse(
82 1
                'API response with error code',
83
                $response
84 1
            );
85
        }
86
87
        $result = $response->json();
88
        if (!$result) {
89
            throw new InvalidResponse(
90
                'API response is not a valid JSON object',
91
                $response->getBody()
92
            );
93
        }
94
95
        $hydrator = new ObjectMap(
96
            [
97
                'first_name' => 'firstname',
98
                'last_name' => 'lastname',
99
                'default_email' => 'email',
100
                'real_name' => 'fullname',
101
                'birthday' => 'birthday',
102
                'login' => 'username',
103
            ]
104
        );
105
106
        return $hydrator->hydrate(new User(), $result);
107
    }
108
}
109