Payload   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 86
rs 10
ccs 36
cts 36
cp 1
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A setStatus() 0 4 1
A getStatus() 0 3 1
A getCode() 0 3 1
A toString() 0 4 1
A setMessage() 0 4 1
A setCode() 0 4 1
A setData() 0 4 1
A toArray() 0 21 5
A getMessage() 0 3 1
A getData() 0 3 1
1
<?php
2
namespace RazonYang\JSend;
3
4
class Payload implements PayloadInterface
5
{
6
    private $status;
7
8 13
    public function getStatus(): string
9
    {
10 13
        return $this->status;
11
    }
12
13 13
    public function setStatus(string $status): PayloadInterface
14
    {
15 13
        $this->status = $status;
16 13
        return $this;
17
    }
18
19
    private $data;
20
21 10
    public function getData()
22
    {
23 10
        return $this->data;
24
    }
25
26 10
    public function setData($data): PayloadInterface
27
    {
28 10
        $this->data = $data;
29 10
        return $this;
30
    }
31
32
    private $message;
33
34 5
    public function getMessage(): ?string
35
    {
36 5
        return $this->message;
37
    }
38
39 5
    public function setMessage(string $message): PayloadInterface
40
    {
41 5
        $this->message = $message;
42 5
        return $this;
43
    }
44
45
    private $code;
46
47 3
    public function getCode(): ?int
48
    {
49 3
        return $this->code;
50
    }
51
52 3
    public function setCode(int $code): PayloadInterface
53
    {
54 3
        $this->code = $code;
55 3
        return $this;
56
    }
57
58 4
    public function toArray(): array
59
    {
60 4
        if ($this->status === Status::SUCCESS || $this->status === Status::FAIL) {
61
            return [
62 2
                'status' => $this->status,
63 2
                'data' => $this->data
64
            ];
65
        }
66
67
        $payload = [
68 2
            'status' => $this->status,
69 2
            'message' => $this->message
70
        ];
71 2
        if ($this->code !== null) {
72 1
            $payload['code'] = $this->code;
73
        }
74 2
        if ($this->data !== null) {
75 1
            $payload['data'] = $this->data;
76
        }
77
78 2
        return $payload;
79
    }
80
81 4
    public function toString(?int $options = null): string
82
    {
83 4
        $options = $options ?? (JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
84 4
        return json_encode($this->toArray(), $options);
85
    }
86
87 4
    public function __toString()
88
    {
89 4
        return $this->toString();
90
    }
91
}
92