Completed
Pull Request — master (#494)
by
unknown
04:59
created

Line::parseAccessTokenResponse()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 30
rs 8.439
cc 6
eloc 17
nc 6
nop 1
1
<?php
2
3
namespace OAuth\OAuth2\Service;
4
5
use OAuth\Common\Exception\Exception;
6
use OAuth\OAuth2\Token\StdOAuth2Token;
7
use OAuth\Common\Http\Exception\TokenResponseException;
8
use OAuth\Common\Http\Uri\Uri;
9
use OAuth\Common\Consumer\CredentialsInterface;
10
use OAuth\Common\Http\Client\ClientInterface;
11
use OAuth\Common\Storage\TokenStorageInterface;
12
use OAuth\Common\Http\Uri\UriInterface;
13
14
class Line extends AbstractService
15
{
16
    /**
17
     * Line www url - used to build dialog urls
18
     */
19
    const WWW_URL = 'https://access.line.me/';
20
21
    const SCOPE_NULL = 'null';
22
23
    public function __construct(
24
        CredentialsInterface $credentials,
25
        ClientInterface $httpClient,
26
        TokenStorageInterface $storage,
27
        $scopes = array(),
28
        UriInterface $baseApiUri = null,
29
        $apiVersion = ""
30
    ) {
31
        parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true, $apiVersion);
32
33
        if (null === $baseApiUri) {
34
            $this->baseApiUri = new Uri('https://api.line.me'.$this->getApiVersionString().'/');
35
        }
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getAuthorizationEndpoint()
42
    {
43
        return new Uri('https://access.line.me'.$this->getApiVersionString().'/dialog/oauth/weblogin');
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getAccessTokenEndpoint()
50
    {
51
        return new Uri('https://api.line.me'.$this->getApiVersionString().'/oauth/access_token');
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function parseAccessTokenResponse($responseBody)
58
    {
59
        // Line gives us a query string ... Oh wait. JSON is too simple, understand ?
60
        parse_str($responseBody, $data);
61
62
        if (null === $data || !is_array($data)) {
63
            throw new TokenResponseException('Unable to parse response.');
64
        } elseif (isset($data['error'])) {
65
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
66
        }
67
68
        $token = new StdOAuth2Token();
69
        $token->setAccessToken($data['access_token']);
70
71
        if (isset($data['expires_in'])) {
72
            $token->setLifeTime($data['expires_in']);
73
        }
74
75
        if (isset($data['refresh_token'])) {
76
            $token->setRefreshToken($data['refresh_token']);
77
            unset($data['refresh_token']);
78
        }
79
80
        unset($data['access_token']);
81
        unset($data['expires_in']);
82
83
        $token->setExtraParams($data);
84
85
        return $token;
86
    }
87
88
    public function getDialogUri($dialogPath, array $parameters)
89
    {
90
        if (!isset($parameters['redirect_uri'])) {
91
            throw new Exception("Redirect uri is mandatory for this request");
92
        }
93
        $parameters['app_id'] = $this->credentials->getConsumerId();
94
        $baseUrl = self::WWW_URL .$this->getApiVersionString(). '/dialog/' . $dialogPath;
95
        $query = http_build_query($parameters);
96
        return new Uri($baseUrl . '?' . $query);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function getApiVersionString()
103
    {
104
        return empty($this->apiVersion) ? '' : '/v' . $this->apiVersion;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    protected function getScopesDelimiter()
111
    {
112
        return ',';
113
    }
114
115
}