Passed
Pull Request — master (#4)
by Randy
02:01
created

Status::isSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Demv\JSend;
4
5
/**
6
 * Class Status
7
 * @package Demv\JSend
8
 */
9
10
use function Dgame\Ensurance\ensure;
11
12
/**
13
 * Class Status
14
 * @package Demv\JSend
15
 */
16
final class Status implements StatusInterface
17
{
18
    /**
19
     * @var Status[]
20
     */
21
    private static $instances = [];
22
    /**
23
     * @var string
24
     */
25
    private $status;
26
27
    /**
28
     * Status constructor.
29
     *
30
     * @param string $status
31
     */
32
    private function __construct(string $status)
33
    {
34
        $this->status = $status;
35
    }
36
37
    /**
38
     * @param string $status
39
     *
40
     * @return Status
41
     */
42
    public static function instance(string $status): self
43
    {
44
        ensure($status)->isIn([self::STATUS_SUCCESS, self::STATUS_FAIL, self::STATUS_ERROR])
45
                       ->orThrow('Expected valid status');
46
        if (!array_key_exists($status, self::$instances)) {
47
            self::$instances[$status] = new self($status);
48
        }
49
50
        return self::$instances[$status];
51
    }
52
53
    /**
54
     * @return StatusInterface
55
     */
56
    public static function success(): StatusInterface
57
    {
58
        return self::instance(self::STATUS_SUCCESS);
59
    }
60
61
    /**
62
     * @return StatusInterface
63
     */
64
    public static function fail(): StatusInterface
65
    {
66
        return self::instance(self::STATUS_FAIL);
67
    }
68
69
    /**
70
     * @return StatusInterface
71
     */
72
    public static function error(): StatusInterface
73
    {
74
        return self::instance(self::STATUS_ERROR);
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isFail(): bool
81
    {
82
        return $this->status === self::STATUS_FAIL;
83
    }
84
85
    /**
86
     * @return bool
87
     */
88
    public function isSuccess(): bool
89
    {
90
        return $this->status === self::STATUS_SUCCESS;
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isError(): bool
97
    {
98
        return $this->status === self::STATUS_ERROR;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function __toString(): string
105
    {
106
        return $this->status;
107
    }
108
}
109