Code Duplication    Length = 65-65 lines in 2 locations

src/Yandex/OAuth/OAuthClient.php 2 locations

@@ 199-263 (lines=65) @@
196
     *
197
     * @throws AuthRequestException on a known request error
198
     */
199
    public function requestAccessToken($code)
200
    {
201
        $client = $this->getClient();
202
203
        try {
204
            $response = $client->request(
205
                'POST',
206
                '/token',
207
                [
208
                    'auth' => [
209
                        $this->clientId,
210
                        $this->clientSecret
211
                    ],
212
                    'form_params' => [
213
                        'grant_type' => 'authorization_code',
214
                        'code' => $code,
215
                        'client_id' => $this->clientId,
216
                        'client_secret' => $this->clientSecret
217
                    ]
218
                ]
219
            );
220
        } catch (ClientException $ex) {
221
            $result = $this->getDecodedBody($ex->getResponse()->getBody());
222
223
            if (is_array($result) && isset($result['error'])) {
224
                // handle a service error message
225
                $message = 'Service responsed with error code "' . $result['error'] . '".';
226
227
                if (isset($result['error_description']) && $result['error_description']) {
228
                    $message .= ' Description "' . $result['error_description'] . '".';
229
                }
230
                throw new AuthRequestException($message, 0, $ex);
231
            }
232
233
            // unknown error. not parsed error
234
            throw $ex;
235
        }
236
237
        try {
238
            $result = $this->getDecodedBody($response->getBody());
239
        } catch (\RuntimeException $ex) {
240
            throw new AuthResponseException('Server response can\'t be parsed', 0, $ex);
241
        }
242
243
        if (!is_array($result)) {
244
            throw new AuthResponseException('Server response has unknown format');
245
        }
246
247
        if (!isset($result['access_token'])) {
248
            throw new AuthResponseException('Server response doesn\'t contain access token');
249
        }
250
251
        $this->setAccessToken($result['access_token']);
252
253
        $this->setRefreshToken($result['refresh_token']);
254
255
        $lifetimeInSeconds = $result['expires_in'];
256
257
        $expireDateTime = new \DateTime();
258
        $expireDateTime->add(new \DateInterval('PT' . $lifetimeInSeconds . 'S'));
259
260
        $this->setExpiresIn($expireDateTime);
261
262
        return $this;
263
    }
264
265
266
@@ 270-334 (lines=65) @@
267
268
269
270
    public function refreshAccessToken(string $refreshToken)
271
    {
272
        $client = $this->getClient();
273
274
        try {
275
            $response = $client->request(
276
                'POST',
277
                '/token',
278
                [
279
                    'auth' => [
280
                        $this->clientId,
281
                        $this->clientSecret
282
                    ],
283
                    'form_params' => [
284
                        'refresh_token' => $refreshToken,
285
                        'grant_type' => 'refresh_token',
286
                        'client_id' => $this->clientId,
287
                        'client_secret' => $this->clientSecret
288
                    ]
289
                ]
290
            );
291
        } catch (ClientException $ex) {
292
            $result = $this->getDecodedBody($ex->getResponse()->getBody());
293
294
            if (is_array($result) && isset($result['error'])) {
295
                // handle a service error message
296
                $message = 'Service responsed with error code "' . $result['error'] . '".';
297
298
                if (isset($result['error_description']) && $result['error_description']) {
299
                    $message .= ' Description "' . $result['error_description'] . '".';
300
                }
301
                throw new AuthRequestException($message, 0, $ex);
302
            }
303
304
            // unknown error. not parsed error
305
            throw $ex;
306
        }
307
308
        try {
309
            $result = $this->getDecodedBody($response->getBody());
310
        } catch (\RuntimeException $ex) {
311
            throw new AuthResponseException('Server response can\'t be parsed', 0, $ex);
312
        }
313
314
        if (!is_array($result)) {
315
            throw new AuthResponseException('Server response has unknown format');
316
        }
317
318
        if (!isset($result['access_token'])) {
319
            throw new AuthResponseException('Server response doesn\'t contain access token');
320
        }
321
322
        $this->setAccessToken($result['access_token']);
323
324
        $this->setRefreshToken($result['refresh_token']);
325
326
        $lifetimeInSeconds = $result['expires_in'];
327
328
        $expireDateTime = new \DateTime();
329
        $expireDateTime->add(new \DateInterval('PT' . $lifetimeInSeconds . 'S'));
330
331
        $this->setExpiresIn($expireDateTime);
332
333
        return $this;
334
    }
335
336
}
337