Completed
Branch master (bda8d5)
by Дмитрий
02:19
created

Yahoo::getIdentity()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 63
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 12.5143

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 35
dl 0
loc 63
rs 8.4266
c 3
b 1
f 1
ccs 15
cts 29
cp 0.5172
cc 7
nc 18
nop 1
crap 12.5143

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * SocialConnect project
4
 *
5
 * @author: Bogdan Popa https://github.com/icex <[email protected]>
6
 */
7
8
namespace SocialConnect\OAuth2\Provider;
9
10
use SocialConnect\Common\Http\Client\Client;
11
use SocialConnect\Provider\AccessTokenInterface;
12
use SocialConnect\Provider\Exception\InvalidAccessToken;
13
use SocialConnect\Provider\Exception\InvalidResponse;
14
use SocialConnect\Common\Entity\User;
15
use SocialConnect\Common\Hydrator\ObjectMap;
16
use SocialConnect\OAuth2\AccessToken;
17
18
class Yahoo extends \SocialConnect\OAuth2\AbstractProvider
19
{
20
    const NAME = 'yahoo';
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 3
    public function getBaseUri()
26
    {
27 3
        return 'https://social.yahooapis.com/v1/';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function getAuthorizeUri()
34
    {
35 2
        return 'https://api.login.yahoo.com/oauth2/request_auth';
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function getRequestTokenUri()
42
    {
43 2
        return 'https://api.login.yahoo.com/oauth2/get_token';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function getName()
50
    {
51 3
        return self::NAME;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3
    public function parseToken($body)
58
    {
59 3
        $result = json_decode($body, true);
60 3
        if ($result) {
61 1
            $token = new AccessToken($result);
62 1
            $token->setUid($result['xoauth_yahoo_guid']);
63
64 1
            return $token;
65
        }
66
67 2
        throw new InvalidAccessToken('AccessToken is not a valid JSON');
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function getIdentity(AccessTokenInterface $accessToken)
74
    {
75 2
        $response = $this->httpClient->request(
76 2
            $this->getBaseUri() . "user/{$accessToken->getUserId()}/profile",
77
            [
78 2
                'format' => 'json',
79
            ],
80 2
            Client::GET,
81
            [
82 2
                'Authorization' => 'Bearer ' . $accessToken->getToken(),
83
            ]
84
        );
85
86 2
        if (!$response->isSuccess()) {
87 1
            throw new InvalidResponse(
88 1
                'API response with error code',
89 1
                $response
90
            );
91
        }
92
93 1
        $result = $response->json();
94 1
        if (!$result) {
95 1
            throw new InvalidResponse(
96 1
                'API response is not a valid JSON object',
97 1
                $response
98
            );
99
        }
100
101
        $result = $result->profile;
102
103
        if (isset($result->image)) {
104
            $result->image = $result->image->imageUrl;
105
        }
106
107
        if (isset($result->emails)) {
108
            // first one should do it, should be the default one
109
            $result->email = reset($result->emails);
110
            $result->email = $result->email->handle;
111
        }
112
113
        if (isset($result->ims)) {
114
            $result->username = reset($result->ims);
115
            $result->username = $result->username->handle;
116
        }
117
118
        if (isset($result->birthdate)) {
119
            $result->birth_date = date('Y-m-d', strtotime($result->birthdate . '/' . $result->birthYear));
120
        }
121
122
        $hydrator = new ObjectMap(
123
            [
124
                'guid'       => 'id',
125
                'image'      => 'picture',
126
                'email'      => 'email',
127
                'givenName'  => 'firstname',
128
                'familyName' => 'lastname',
129
                'username'   => 'username',
130
                'gender'     => 'gender',
131
                'birth_date' => 'birth_date',
132
            ]
133
        );
134
135
        return $hydrator->hydrate(new User(), $result);
136
    }
137
}
138