ClientExceptionHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handleClientException() 0 8 2
A handleRateLimitException() 0 12 2
1
<?php
2
3
namespace LoLApi\Handler;
4
5
use GuzzleHttp\Exception\ClientException;
6
use LoLApi\Exception\AbstractRateLimitException;
7
use LoLApi\Exception\ServiceRateLimitException;
8
use LoLApi\Exception\UserRateLimitException;
9
10
/**
11
 * Class ClientExceptionHandler
12
 *
13
 * @package LoLApi\Handler
14
 */
15
class ClientExceptionHandler
16
{
17
    const HEADER_RATE_LIMIT_TYPE = 'X-Rate-Limit-Type';
18
    const HEADER_RETRY_AFTER = 'Retry-After';
19
    const RATE_LIMIT_TYPE_USER = 'user';
20
    const RATE_LIMIT_TYPE_SERVICE = 'service';
21
22
    /**
23
     * @param ClientException $e
24
     *
25
     * @return ClientException|AbstractRateLimitException
26
     */
27 3
    public function handleClientException(ClientException $e)
28
    {
29 3
        if ($e->getResponse()->getStatusCode() === 429) {
30 2
            return $this->handleRateLimitException($e);
31
        }
32
33 1
        return $e;
34
    }
35
36
    /**
37
     * @param ClientException $e
38
     *
39
     * @return AbstractRateLimitException
40
     */
41 2
    protected function handleRateLimitException(ClientException $e)
42
    {
43 2
        if ($e->getResponse()->getHeader(self::HEADER_RATE_LIMIT_TYPE)[0] === self::RATE_LIMIT_TYPE_USER) {
44 1
            $exception = new UserRateLimitException();
45 1
        } else {
46 1
            $exception = new ServiceRateLimitException();
47
        }
48
49
        return $exception
50 2
            ->setClientException($e)
51 2
            ->setRetryAfter($e->getResponse()->getHeader(self::HEADER_RETRY_AFTER)[0]);
52
    }
53
}
54