Test Failed
Push — master ( 2b935e...e07ba9 )
by Lyal
02:07
created

RequestErrorHandler::handle404()   A

Complexity

Conditions 1
Paths 0

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 0
nop 0
1
<?php
2
3
4
namespace Lyal\Checkr;
5
6
7
use Lyal\Checkr\Exceptions\Client\BadRequest;
8
use Lyal\Checkr\Exceptions\Client\Conflict;
9
use Lyal\Checkr\Exceptions\Client\Forbidden;
10
use Lyal\Checkr\Exceptions\Client\NotFound;
11
use Lyal\Checkr\Exceptions\Client\Unauthorized;
12
use Lyal\Checkr\Exceptions\Server\InternalServerError;
13
use Lyal\Checkr\Exceptions\UnhandledRequestError;
14
15
class RequestErrorHandler
16
{
17
    private $exception;
18
    private $body;
19
20
    public function __construct($exception)
21
    {
22
        $this->exception = $exception;
23
        $this->body = $exception->getResponse()->getBody();
24
    }
25
26
    public function handleError()
27
    {
28
        $errorCode = $this->exception->getResponse()->getStatusCode();
29
30
        if (method_exists($this, 'handle' . $errorCode)) {
31
            $this->{'handle' . $errorCode}();
32
        }
33
        $this->handleUnknown();
34
35
    }
36
37
    protected function handle400()
38
    {
39
        throw new BadRequest($this->body);
40
    }
41
42
    protected function handle401()
43
    {
44
        throw new Unauthorized($this->body);
45
    }
46
47
    protected function handle403()
48
    {
49
        throw new Forbidden($this->body);
50
    }
51
52
    protected function handle404()
53
    {
54
        throw new NotFound($this->body);
55
    }
56
57
    protected function handle409()
58
    {
59
        throw new Conflict($this->body);
60
    }
61
62
    protected function handle500()
63
    {
64
        throw new InternalServerError($this->body);
65
    }
66
67
68
    private function handleUnknown()
69
    {
70
        throw new UnhandledRequestError($this->exception->getResponse()->getStatusCode(), $this->body);
71
    }
72
73
74
}