Passed
Push — master ( 9c6c89...5e1c69 )
by Artem
01:48 queued 18s
created

ErrorHandler::handleResponse()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 19
rs 8.8333
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 500:
38
                throw new DomainExceptions\ServerException();
39
            default:
40
                throw new DomainExceptions\UnknownErrorException();
41
        }
42
    }
43
}
44