ErrorHandler::handleResponse()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 18
nc 8
nop 1
dl 0
loc 21
rs 8.4444
c 1
b 0
f 0
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