Completed
Push — master ( f43770...25a62d )
by Toby
47:32 queued 45:20
created

ErrorHandler::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 3.3332
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use Exception;
15
use RuntimeException;
16
use Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface;
17
18
class ErrorHandler
19
{
20
    /**
21
     * Stores the valid handlers.
22
     *
23
     * @var \Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface[]
24
     */
25
    private $handlers = [];
26
27
    /**
28
     * Handle the exception provided.
29
     *
30
     * @param Exception $e
31
     *
32
     * @throws RuntimeException
33
     *
34
     * @return \Tobscure\JsonApi\Exception\Handler\ResponseBag
35
     */
36 3
    public function handle(Exception $e)
37
    {
38 3
        foreach ($this->handlers as $handler) {
39
            if ($handler->manages($e)) {
40
                return $handler->handle($e);
41
            }
42 3
        }
43
44 3
        throw new RuntimeException('Exception handler for '.get_class($e).' not found.');
45
    }
46
47
    /**
48
     * Register a new exception handler.
49
     *
50
     * @param \Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface $handler
51
     *
52
     * @return void
53
     */
54
    public function registerHandler(ExceptionHandlerInterface $handler)
55
    {
56
        $this->handlers[] = $handler;
57
    }
58
}
59