Completed
Pull Request — master (#549)
by Georgio
01:57
created

Yandex::getAuthorizationEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\OAuth2\Token\StdOAuth2Token;
6
use OAuth\Common\Http\Exception\TokenResponseException;
7
use OAuth\Common\Http\Uri\Uri;
8
use OAuth\Common\Consumer\CredentialsInterface;
9
use OAuth\Common\Http\Client\ClientInterface;
10
use OAuth\Common\Storage\TokenStorageInterface;
11
use OAuth\Common\Http\Uri\UriInterface;
12
13
class Yandex extends AbstractService
14
{
15
    /**
16
     * Defined scopes
17
     *
18
     * @link https://tech.yandex.ru/money/doc/dg/concepts/protocol-rights-docpage/
19
     */
20
    const SCOPE_ACCOUNT_INFO         = 'account-info';
21
22
    public function __construct(
23
        CredentialsInterface $credentials,
24
        ClientInterface $httpClient,
25
        TokenStorageInterface $storage,
26
        $scopes = array(),
27
        UriInterface $baseApiUri = null
28
    ) {
29
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
30
31
        if (null === $baseApiUri) {
32
            $this->baseApiUri = new Uri('https://login.yandex.ru/');
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function getAuthorizationEndpoint()
40
    {
41
        return new Uri('https://oauth.yandex.ru/authorize');
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getAccessTokenEndpoint()
48
    {
49
        return new Uri('https://oauth.yandex.ru/token');
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function parseAccessTokenResponse($responseBody)
56
    {
57
        $data = json_decode($responseBody, true);
58
        if (null === $data || !is_array($data)) {
59
            throw new TokenResponseException('Unable to parse response.');
60
        } elseif (isset($data['error'])) {
61
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
62
        }
63
64
        $token = new StdOAuth2Token();
65
        $token->setAccessToken($data['access_token']);
66
        $token->setLifeTime($data['expires_in']);
67
68
        if (isset($data['refresh_token'])) {
69
            $token->setRefreshToken($data['refresh_token']);
70
            unset($data['refresh_token']);
71
        }
72
73
        unset($data['access_token']);
74
        unset($data['expires_in']);
75
        $token->setExtraParams($data);
76
77
        return $token;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function getAuthorizationMethod()
84
    {
85
        return static::AUTHORIZATION_METHOD_QUERY_STRING_V5;
86
    }
87
88
89
}
90