AuthorizationHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 10
c 3
b 0
f 2
lcom 1
cbo 6
dl 0
loc 127
ccs 40
cts 42
cp 0.9524
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A checkTokenExpired() 0 10 2
A getAuthorizationUrl() 0 19 2
B requestAccessToken() 0 29 3
A refreshToken() 0 17 2
1
<?php
2
3
namespace Mechpave\ImgurClient\Authorization;
4
5
use Mechpave\ImgurClient\Http\HttpClientInterface;
6
use Mechpave\ImgurClient\ImgurClient;
7
use Mechpave\ImgurClient\Mapper\TokenMapper;
8
use Mechpave\ImgurClient\Model\AuthorizationModel;
9
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
10
11
/**
12
 * Class AuthorizationHandler
13
 * @package Mechpave\ImgurClient\Authorization
14
 */
15
class AuthorizationHandler implements AuthorizationHandlerInterface
16
{
17
    use ContainerAwareTrait;
18
19
    /**
20
     * @var ImgurClient
21
     */
22
    protected $client;
23
24
    /**
25
     * @var HttpClientInterface
26
     */
27
    protected $httpClient;
28
29
    /**
30
     * @var TokenMapper
31
     */
32
    protected $tokenMapper;
33
34
    /**
35
     * AuthorizationHandler constructor.
36
     * @param ImgurClient $client
37
     * @param HttpClientInterface $httpClient
38
     * @param TokenMapper $tokenMapper
39
     */
40 8
    public function __construct(
41
        ImgurClient $client,
42
        HttpClientInterface $httpClient,
43
        TokenMapper $tokenMapper)
44
    {
45 8
        $this->client = $client;
46 8
        $this->httpClient = $httpClient;
47 8
        $this->tokenMapper = $tokenMapper;
48 8
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53 1
    public function getAuthorizationUrl(
54
        $responseType = AuthorizationModel::RESPONSE_CODE,
55
        $state = null
56
    ) {
57 1
        $uri = AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_AUTHORIZATION;
58
59
        $queryParams = [
60 1
            'client_id' => $this->client->getClientId(),
61 1
            'response_type' => $responseType,
62
        ];
63
64 1
        if ($state) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $state of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
65 1
            $queryParams['state'] = $state;
66
        }
67
68 1
        $uri .= '?' . http_build_query($queryParams);
69
70 1
        return $uri;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 2
    public function requestAccessToken(
77
        $code,
78
        $grantType = AuthorizationModel::GRANT_CODE
79
    ) {
80
        $postData = [
81 2
            'client_id' => $this->client->getClientId(),
82 2
            'client_secret' => $this->client->getClientSecret(),
83 2
            'grant_type' => $grantType,
84
        ];
85
86
        switch ($grantType) {
87 2
            case AuthorizationModel::GRANT_CODE:
88 1
                $postData['code'] = $code;
89 1
                break;
90 1
            case AuthorizationModel::GRANT_PIN:
91
                $postData['pin'] = $code;
92
                break;
93
            default:
94 1
                throw new \InvalidArgumentException('Invalid grant_type provided');
95
                break;
0 ignored issues
show
Unused Code introduced by
break; 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...
96
        }
97
98 1
        $response = $this->httpClient->post(
99 1
            AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_TOKEN,
100
            $postData
101
        );
102
103 1
        return $this->tokenMapper->buildToken($response->getBody());
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109 2
    public function refreshToken()
110
    {
111 2
        if (!$this->client->getToken()) {
112 1
            throw new \Exception('Token is not set');
113
        }
114 1
        $response = $this->httpClient->post(
115 1
            AuthorizationModel::ENDPOINT_BASE . AuthorizationModel::ENDPOINT_TOKEN,
116
            [
117 1
                'refresh_token' => $this->client->getToken()->getRefreshToken(),
118 1
                'client_id' => $this->client->getClientId(),
119 1
                'client_secret' => $this->client->getClientSecret(),
120 1
                'grant_type' => AuthorizationModel::GRANT_REFRESH,
121
            ]
122
        );
123
124 1
        return $this->tokenMapper->buildToken($response->getBody());
125
    }
126
127
    /**
128
     * @inheritdoc
129
     * @throws \Exception
130
     */
131 3
    public function checkTokenExpired()
132
    {
133 3
        $token = $this->client->getToken();
134
135 3
        if (!$token) {
136 1
            throw new \Exception('Token is not set');
137
        }
138
139 2
        return $token->getExpiresAt() < time();
140
    }
141
}