ErrorHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 44.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 41
ccs 4
cts 9
cp 0.4444
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 10 3
A registerHandler() 0 4 1
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