Completed
Push — master ( 1fa76a...9562c6 )
by Randy
15s queued 12s
created

Status::success()   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
use function Dgame\Ensurance\ensure;
6
7
/**
8
 * Class Status
9
 * @package Demv\JSend
10
 */
11
final class Status implements StatusInterface
12
{
13
    public const DEFAULT_MAPPING = [
14
        -1 => self::STATUS_ERROR,
15
        0  => self::STATUS_FAIL,
16
        1  => self::STATUS_SUCCESS
17
    ];
18
19
    /**
20
     * @var StatusInterface[]
21
     */
22
    private static $instances = [];
23
24
    /**
25
     * @var string
26
     */
27
    private $status;
28
29
    /**
30
     * Status constructor.
31
     *
32
     * @param string $status
33
     */
34
    private function __construct(string $status)
35
    {
36
        $this->status = $status;
37
    }
38
39
    /**
40
     * @param string $status
41
     *
42
     * @return StatusInterface
43
     */
44
    public static function instance(string $status): StatusInterface
45
    {
46
        ensure($status)->isIn([self::STATUS_SUCCESS, self::STATUS_FAIL, self::STATUS_ERROR])->orThrow('Expected valid status');
47
        if (!array_key_exists($status, self::$instances)) {
48
            self::$instances[$status] = new self($status);
49
        }
50
51
        return self::$instances[$status];
52
    }
53
54
    /**
55
     * @return StatusInterface
56
     */
57
    public static function success(): StatusInterface
58
    {
59
        return self::instance(self::STATUS_SUCCESS);
60
    }
61
62
    /**
63
     * @return StatusInterface
64
     */
65
    public static function fail(): StatusInterface
66
    {
67
        return self::instance(self::STATUS_FAIL);
68
    }
69
70
    /**
71
     * @return StatusInterface
72
     */
73
    public static function error(): StatusInterface
74
    {
75
        return self::instance(self::STATUS_ERROR);
76
    }
77
78
    /**
79
     * @param int   $value
80
     * @param array $mapping
81
     *
82
     * @return StatusInterface
83
     */
84
    public static function translate(int $value, array $mapping = self::DEFAULT_MAPPING): StatusInterface
85
    {
86
        ensure($value)->isKeyOf($mapping)->orThrow('Cannot map %d, there is not mapping available', $value);
87
        $status = $mapping[$value];
88
        ensure($status)->isNotEmpty()->isString()->orThrow('Status must be string');
89
90
        return self::instance($status);
91
    }
92
93
    /**
94
     * @return bool
95
     */
96
    public function isFail(): bool
97
    {
98
        return $this->status === self::STATUS_FAIL;
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function isSuccess(): bool
105
    {
106
        return $this->status === self::STATUS_SUCCESS;
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function isError(): bool
113
    {
114
        return $this->status === self::STATUS_ERROR;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function __toString(): string
121
    {
122
        return $this->status;
123
    }
124
}
125