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

Status::from()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Demv\JSend;
4
5
use function Dgame\Ensurance\ensure;
6
7
/**
8
 * Class Status
9
 * @package Demv\JSend
10
 */
11
final class Status
12
{
13
    private const SUCCESS = 'success';
14
    private const FAIL    = 'fail';
15
    private const ERROR   = 'error';
16
17
    /**
18
     * @var self[]
19
     */
20
    private static $froms = [];
21
    /**
22
     * @var string
23
     */
24
    private $status;
25
26
    /**
27
     * Status constructor.
28
     *
29
     * @param string $status
30
     */
31
    private function __construct(string $status)
32
    {
33
        $this->status = $status;
34
    }
35
36
    /**
37
     * @param string $status
38
     *
39
     * @return self
40
     */
41
    public static function from(string $status): self
42
    {
43
        if (is_numeric($status)) {
44
            return self::fromStatusCode($status);
45
        }
46
47
        ensure($status)->isIn([self::SUCCESS, self::FAIL, self::ERROR])->orThrow('Expected valid status');
48
        if (!array_key_exists($status, self::$froms)) {
49
            self::$froms[$status] = new self($status);
50
        }
51
52
        return self::$froms[$status];
53
    }
54
55
    /**
56
     * @param int $code
57
     *
58
     * @return Status
59
     */
60
    public static function fromStatusCode(int $code): self
61
    {
62
        ensure($code)->isBetween(100, 511)->orThrow('Invalid Http-Status-Code: %d', $code);
63
64
        switch (true) {
65
            case $code >= 200 && $code < 300:
66
                return self::success();
67
            case $code < 500:
68
                return self::fail();
69
            default:
70
                return self::error();
71
        }
72
    }
73
74
    /**
75
     * @return self
76
     */
77
    public static function success(): self
78
    {
79
        return self::from(self::SUCCESS);
80
    }
81
82
    /**
83
     * @return self
84
     */
85
    public static function fail(): self
86
    {
87
        return self::from(self::FAIL);
88
    }
89
90
    /**
91
     * @return self
92
     */
93
    public static function error(): self
94
    {
95
        return self::from(self::ERROR);
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    public function isFail(): bool
102
    {
103
        return $this->status === self::FAIL;
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isSuccess(): bool
110
    {
111
        return $this->status === self::SUCCESS;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function isError(): bool
118
    {
119
        return $this->status === self::ERROR;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function __toString(): string
126
    {
127
        return $this->status;
128
    }
129
}
130