ErrorDoc::setDataDoc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
namespace Yoanm\JsonRpcServerDoc\Domain\Model;
3
4
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\TypeDoc;
5
6
/**
7
 * Class ErrorDoc
8
 */
9
class ErrorDoc
10
{
11
    /** @var string */
12
    private $title;
13
    /** @var int */
14
    private $code;
15
    /** @var string|null */
16
    private $message = null;
17
    /** @var TypeDoc|null */
18
    private $dataDoc = null;
19
    /** @var string */
20
    private $identifier;
21
22
    /**
23
     * @param string       $title
24
     * @param int          $code
25
     * @param string|null  $message
26
     * @param TypeDoc|null $dataDoc
27
     * @param string|null  $identifier
28
     */
29 23
    public function __construct(
30
        string $title,
31
        int $code,
32
        string $message = null,
33
        TypeDoc $dataDoc = null,
34
        string $identifier = null
35
    ) {
36 23
        $this->title = $title;
37 23
        $this->code = $code;
38 23
        $this->message = $message;
39 23
        $this->dataDoc = $dataDoc;
40
        // Use title and code as id if not provided
41 23
        $this->setIdentifier($identifier ?? $title.((string)$code));
42
    }
43
44
    /**
45
     * @param string $message
46
     *
47
     * @return self
48
     */
49 2
    public function setMessage(string $message): self
50
    {
51 2
        $this->message = $message;
52
53 2
        return $this;
54
    }
55
56
    /**
57
     * @param string $identifier
58
     *
59
     * @return self
60
     */
61 23
    public function setIdentifier(string $identifier) : self
62
    {
63
        // Sanitize Identifier => remove space and slashes
64 23
        $this->identifier = strtr(
65 23
            ucwords(strtr(
66 23
                $identifier,
67 23
                ['_' => ' ', '/' => '-', '.' => '_ ', '\\' => '_ ']
68 23
            )),
69 23
            [' ' => '']
70 23
        );
71
72 23
        return $this;
73
    }
74
75
    /**
76
     * @param TypeDoc $dataDoc
77
     *
78
     * @return self
79
     */
80 1
    public function setDataDoc(TypeDoc $dataDoc) : self
81
    {
82 1
        $this->dataDoc = $dataDoc;
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * @return string
89
     */
90 11
    public function getTitle() : string
91
    {
92 11
        return $this->title;
93
    }
94
95
    /**
96
     * @return int
97
     */
98 11
    public function getCode() : int
99
    {
100 11
        return $this->code;
101
    }
102
103
    /**
104
     * @return string|null
105
     */
106 13
    public function getMessage() : ?string
107
    {
108 13
        return $this->message;
109
    }
110
111
    /**
112
     * @return TypeDoc|null
113
     */
114 13
    public function getDataDoc() : ?TypeDoc
115
    {
116 13
        return $this->dataDoc;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 17
    public function getIdentifier() : string
123
    {
124 17
        return $this->identifier;
125
    }
126
}
127