Completed
Pull Request — master (#494)
by
unknown
04:47 queued 53s
created

Line::getDialogUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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