RequestErrorHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 10
eloc 16
c 2
b 2
f 0
dl 0
loc 54
rs 10

9 Methods

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