ErrorHandler   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 30
rs 10
c 1
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B handleResponse() 0 21 8
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoman4eg\Nalog;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Shoman4eg\Nalog\Exception\Domain as DomainExceptions;
8
use Shoman4eg\Nalog\Exception\DomainException;
9
10
/**
11
 * @author Artem Dubinin <[email protected]>
12
 */
13
final class ErrorHandler
14
{
15
    /**
16
     * Handle HTTP errors.
17
     *
18
     * Call is controlled by the specific API methods.
19
     *
20
     * @throws DomainException
21
     */
22
    public function handleResponse(ResponseInterface $response): void
23
    {
24
        $body = (string)$response->getBody();
25
26
        switch ($response->getStatusCode()) {
27
            case 400:
28
                throw new DomainExceptions\ValidationException($body);
29
            case 401:
30
                throw new DomainExceptions\UnauthorizedException($body);
31
            case 403:
32
                throw new DomainExceptions\ForbiddenException();
33
            case 404:
34
                throw new DomainExceptions\NotFoundException();
35
            case 406:
36
                throw new DomainExceptions\ClientException('Wrong Accept headers');
37
            case 422:
38
                throw new DomainExceptions\PhoneException($body);
39
            case 500:
40
                throw new DomainExceptions\ServerException();
41
            default:
42
                throw new DomainExceptions\UnknownErrorException();
43
        }
44
    }
45
}
46