Completed
Pull Request — master (#494)
by
unknown
03:31
created

Line::parseAccessTokenResponse()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 31
rs 8.439
cc 6
eloc 18
nc 1
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
    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
        parse_str($responseBody, $data);
62
        echo "<pre>";print_r($data);echo "</pre>";exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method parseAccessTokenResponse() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
63
64
        if (null === $data || !is_array($data)) {
0 ignored issues
show
Unused Code introduced by
if (null === $data || !i...data['error'] . '"'); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
65
            throw new TokenResponseException('Unable to parse response.');
66
        } elseif (isset($data['error'])) {
67
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
68
        }
69
70
        $token = new StdOAuth2Token();
71
        $token->setAccessToken($data['access_token']);
72
73
        if (isset($data['expires_in'])) {
74
            $token->setLifeTime($data['expires_in']);
75
        }
76
77
        if (isset($data['refresh_token'])) {
78
            $token->setRefreshToken($data['refresh_token']);
79
            unset($data['refresh_token']);
80
        }
81
82
        unset($data['access_token']);
83
        unset($data['expires_in']);
84
85
        $token->setExtraParams($data);
86
87
        return $token;
88
    }
89
90
    public function getDialogUri($dialogPath, array $parameters)
91
    {
92
        if (!isset($parameters['redirect_uri'])) {
93
            throw new Exception("Redirect uri is mandatory for this request");
94
        }
95
        $parameters['app_id'] = $this->credentials->getConsumerId();
96
        $baseUrl = self::WWW_URL .$this->getApiVersionString(). '/dialog/' . $dialogPath;
97
        $query = http_build_query($parameters);
98
        return new Uri($baseUrl . '?' . $query);
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    protected function getApiVersionString()
105
    {
106
        return empty($this->apiVersion) ? '/v1' : '/v' . $this->apiVersion;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    protected function getScopesDelimiter()
113
    {
114
        return ',';
115
    }
116
117
}