1 | <?php |
||
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( |
|
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) { |
|
|
|||
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; |
||
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() |
|
141 | } |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: