ErrorResponseFactory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 15
c 2
b 1
f 0
lcom 1
cbo 6
dl 0
loc 57
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A exposeInternalExceptions() 0 6 1
C createForException() 0 23 13
1
<?php
2
3
namespace Tonic\Component\ApiLayer\JsonRpc\Response;
4
5
use Tonic\Component\ApiLayer\JsonRpc\Method\Exception\InvalidCallableArgumentsException;
6
use Tonic\Component\ApiLayer\JsonRpc\Method\Exception\InvalidMethodParametersException;
7
use Tonic\Component\ApiLayer\JsonRpc\Method\Exception\MethodNotFoundException;
8
use Tonic\Component\ApiLayer\JsonRpc\Request\Exception\InvalidRequestException;
9
use Tonic\Component\ApiLayer\JsonRpc\Request\Exception\ParseException;
10
use Tonic\Component\ApiLayer\JsonRpc\Exception\Exception as ApplicationDefinedException;
11
12
/**
13
 * Factory for error responses.
14
 */
15
class ErrorResponseFactory
16
{
17
    /**
18
     * @var bool
19
     */
20
    private $exposeInternalExceptions = false;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param bool|false $exposeInternalExceptions
26
     */
27
    public function __construct($exposeInternalExceptions = false)
28
    {
29
        $this->exposeInternalExceptions = $exposeInternalExceptions;
30
    }
31
32
    /**
33
     * @return ErrorResponseFactory
34
     */
35
    public function exposeInternalExceptions()
36
    {
37
        $this->exposeInternalExceptions = true;
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param \Exception  $exception
44
     * @param string|null $id
45
     *
46
     * @return ErrorResponse
47
     */
48
    public function createForException(\Exception $exception, $id = null)
49
    {
50
        switch (true) {
51
            case $exception instanceof ParseException:
52
                return ErrorResponse::parseError($exception->getMessage() ?: 'Parse error');
53
            case $exception instanceof InvalidRequestException:
54
                return ErrorResponse::invalidRequestError($exception->getMessage() ?: 'Invalid request');
55
            case $exception instanceof MethodNotFoundException:
56
                return ErrorResponse::methodNotFoundError($id, $exception->getMessage() ?: 'Method not found');
57
            case $exception instanceof InvalidCallableArgumentsException:
58
            case $exception instanceof InvalidMethodParametersException:
59
                return ErrorResponse::invalidParamsError($id, $exception->getMessage() ?: 'Invalid params');
60
            case $exception instanceof ApplicationDefinedException:
61
                return ErrorResponse::applicationDefinedError($id, $exception->getMessage(), $exception->getCode());
62
            case $exception instanceof \Exception:
63
            default:
64
                if ($this->exposeInternalExceptions) {
65
                    return new ErrorResponse($id, new Error($exception->getMessage(), $exception->getCode()));
66
                }
67
68
                return ErrorResponse::applicationError($id);
69
        }
70
    }
71
}
72