Completed
Pull Request — master (#18)
by Randy
01:46
created

JSendError::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Demv\JSend;
4
5
use function Dgame\Ensurance\enforce;
6
use function Dgame\Extraction\export;
7
8
/**
9
 * Class JSendError
10
 * @package Demv\JSend
11
 */
12
final class JSendError implements JSendInterface
13
{
14
    use JSendStatusTrait;
15
    use JSendResponseTrait;
16
    use JSendJsonTrait;
17
    use ResponseDataTrait;
18
19
    /**
20
     * JSendError constructor.
21
     *
22
     * @param string   $message
23
     * @param int|null $code
24
     */
25
    public function __construct(string $message, int $code = null)
26
    {
27
        $this->status  = Status::error();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Demv\JSend\Status::error() of type object<self> is incompatible with the declared type object<Demv\JSend\Status> of property $status.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->message = $message;
29
        $this->code    = $code;
30
31
        $this->initResponseData();
32
    }
33
34
    /**
35
     * @return int
36
     */
37
    public function getDefaultHttpStatusCode(): int
38
    {
39
        return 500;
40
    }
41
42
    /**
43
     * @return int|null
44
     */
45
    public function getCode(): ?int
46
    {
47
        return $this->code;
48
    }
49
50
    /**
51
     * @return null|string
52
     */
53
    public function getMessage(): ?string
54
    {
55
        return $this->getReasonPhrase();
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function asArray(): array
62
    {
63
        return array_filter(
64
            [
65
                'status'  => (string) $this->status,
66
                'message' => $this->message,
67
                'code'    => $this->code
68
            ]
69
        );
70
    }
71
72
    /**
73
     * @param string $json
74
     *
75
     * @return JSendError
76
     * @throws InvalidJsonException
77
     */
78
    public static function decode(string $json): self
79
    {
80
        $decoded = JSend::safeDecode($json);
81
82
        return self::from($decoded);
83
    }
84
85
    /**
86
     * @param array $decoded
87
     *
88
     * @return JSendError
89
     */
90
    public static function from(array $decoded): self
91
    {
92
        [
93
            'status'  => $status,
0 ignored issues
show
Bug introduced by
The variable $status does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
94
            'message' => $message,
0 ignored issues
show
Bug introduced by
The variable $message does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
95
            'code'    => $code
0 ignored issues
show
Bug introduced by
The variable $code does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
96
        ] = export('status', 'message', 'code')->require('status', 'message')->from($decoded);
97
        enforce(Status::from($status)->isError())->orThrow('Decode non-error in JSendError');
98
99
        return new self($message, $code);
100
    }
101
102
    /**
103
     * @param string|null $key
104
     *
105
     * @return bool
106
     */
107
    public function hasData(string $key = null): bool
108
    {
109
        return false;
110
    }
111
112
    /**
113
     * @param string|null $key
114
     * @param mixed|null  $default
115
     *
116
     * @return array|mixed|null
117
     */
118
    public function getData(string $key = null, $default = null)
119
    {
120
        return $key === null ? [] : $default;
121
    }
122
}
123