Yahoo::getIdentity()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.6

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 5
eloc 22
nc 16
nop 1
dl 0
loc 37
ccs 9
cts 15
cp 0.6
crap 6.6
rs 9.2568
c 3
b 1
f 1
1
<?php
2
/**
3
 * SocialConnect project
4
 * @author: Patsura Dmitry https://github.com/ovr <[email protected]>
5
 * @author: Bogdan Popa https://github.com/icex <[email protected]>
6
 */
7
declare(strict_types=1);
8
9
namespace SocialConnect\OAuth2\Provider;
10
11
use SocialConnect\Common\ArrayHydrator;
12
use SocialConnect\Provider\AccessTokenInterface;
13
use SocialConnect\Provider\Exception\InvalidAccessToken;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\OAuth2\AccessToken;
16
17
class Yahoo extends \SocialConnect\OAuth2\AbstractProvider
18
{
19
    const NAME = 'yahoo';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 4
    public function getBaseUri()
25
    {
26 4
        return 'https://social.yahooapis.com/v1/';
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function getAuthorizeUri()
33
    {
34 2
        return 'https://api.login.yahoo.com/oauth2/request_auth';
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function getRequestTokenUri()
41
    {
42 2
        return 'https://api.login.yahoo.com/oauth2/get_token';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 3
    public function getName()
49
    {
50 3
        return self::NAME;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 4
    public function parseToken(string $body)
57
    {
58 4
        if (empty($body)) {
59 1
            throw new InvalidAccessToken('Provider response with empty body');
60
        }
61
62 3
        $result = json_decode($body, true);
63 3
        if ($result) {
64 1
            $token = new AccessToken($result);
65 1
            $token->setUserId((string) $result['xoauth_yahoo_guid']);
66
67 1
            return $token;
68
        }
69
70 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 3
    public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void
77
    {
78 3
        $query['format'] = 'json';
79
80 3
        if ($accessToken) {
81 3
            $headers['Authorization'] = "Bearer {$accessToken->getToken()}";
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 3
    public function getIdentity(AccessTokenInterface $accessToken)
89
    {
90 3
        $response = $this->request('GET', "user/{$accessToken->getUserId()}/profile", [], $accessToken);
91
92 1
        $result = $response['profile'];
93
94 1
        if (isset($result['image'])) {
95
            $result->image = $result['image']['imageUrl'];
96
        }
97
98 1
        if (isset($result['emails'])) {
99
            // first one should do it, should be the default one
100
            $email = reset($result['emails']);
101
            $result['email'] = $email['handle'];
102
        }
103
104 1
        if (isset($result['ims'])) {
105
            $username = reset($result['ims']);
106
            $result->username = $username['handle'];
107
        }
108
109 1
        if (isset($result['birthdate'])) {
110
            $result['birthdate'] = date('Y-m-d', strtotime($result['birthdate'] . '/' . $result['birthdate']));
111
        }
112
113 1
        $hydrator = new ArrayHydrator([
114
            'guid'       => 'id',
115
            'image'      => 'picture',
116
            'email'      => 'email',
117
            'givenName'  => 'firstname',
118
            'familyName' => 'lastname',
119
            'username'   => 'username',
120
            'gender'     => 'gender',
121
            'birth_date' => 'birth_date',
122
        ]);
123
124 1
        return $hydrator->hydrate(new User(), $result);
125
    }
126
}
127