Yahoo::getAccessTokenEndpoint()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Http\Exception\TokenResponseException;
6
use OAuth\Common\Http\Uri\Uri;
7
use OAuth\OAuth2\Token\StdOAuth2Token;
8
9
class Yahoo extends AbstractService
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function getAuthorizationEndpoint()
15
    {
16
        return new Uri('https://api.login.yahoo.com/oauth2/request_auth');
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function getAccessTokenEndpoint()
23
    {
24
        return new Uri('https://api.login.yahoo.com/oauth2/get_token');
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function getAuthorizationMethod()
31
    {
32
        return static::AUTHORIZATION_METHOD_HEADER_BEARER;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    protected function parseAccessTokenResponse($responseBody)
39
    {
40
        $data = json_decode($responseBody, true);
41
42
        if (null === $data || !is_array($data)) {
43
            throw new TokenResponseException('Unable to parse response.');
44
        } elseif (isset($data['error'])) {
45
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
46
        }
47
48
        $token = new StdOAuth2Token();
49
        $token->setAccessToken($data['access_token']);
50
        $token->setLifetime($data['expires_in']);
51
52
        if (isset($data['refresh_token'])) {
53
            $token->setRefreshToken($data['refresh_token']);
54
            unset($data['refresh_token']);
55
        }
56
57
        unset($data['access_token'], $data['expires_in']);
58
59
        $token->setExtraParams($data);
60
61
        return $token;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    protected function getExtraOAuthHeaders()
68
    {
69
        $encodedCredentials = base64_encode(
70
            $this->credentials->getConsumerId() . ':' . $this->credentials->getConsumerSecret()
71
        );
72
73
        return ['Authorization' => 'Basic ' . $encodedCredentials];
74
    }
75
}
76