Error   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 72
ccs 10
cts 18
cp 0.5556
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A exception() 0 4 1
A code() 0 4 1
A message() 0 4 1
A hasData() 0 4 2
A data() 0 6 2
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace PhpJsonRpc\Server\Response;
4
5
use PhpJsonRpc\Server\Response\Contract\ErrorFormatter;
6
use PhpJsonRpc\Server\Error\Exception\ExceptionWithData;
7
8
class Error implements \JsonSerializable
9
{
10
    /**
11
     * @var \Exception
12
     */
13
    protected $exception;
14
15
    /**
16
     * @var ErrorFormatter
17
     */
18
    protected $errorFormatter;
19
20
    /**
21
     * @param \Exception $exception
22
     * @param ErrorFormatter $errorFormatter
23
     */
24 18
    public function __construct(\Exception $exception, ErrorFormatter $errorFormatter)
25
    {
26 18
        $this->exception        = $exception;
27 18
        $this->errorFormatter   = $errorFormatter;
28 18
    }
29
30
    /**
31
     * @return \Exception
32
     */
33 18
    public function exception()
34
    {
35 18
        return $this->exception;
36
    }
37
38
    /**
39
     * @return int|mixed
40
     */
41 12
    public function code()
42
    {
43 12
        return $this->exception()->getCode();
44
    }
45
46
    /**
47
     * @return string
48
     */
49 6
    public function message()
50
    {
51 6
        return $this->exception()->getMessage();
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function hasData()
58
    {
59
        return ($this->exception instanceof ExceptionWithData && $this->exception->getData());
60
    }
61
62
    /**
63
     * @return mixed|null
64
     */
65
    public function data()
66
    {
67
        if ($this->exception instanceof ExceptionWithData) {
68
            return $this->exception->getData();
69
        }
70
    }
71
72
    /**
73
     * @return \stdClass
74
     */
75
    public function jsonSerialize()
76
    {
77
        return $this->errorFormatter->formatError($this);
78
    }
79
}
80