Completed
Pull Request — master (#30)
by
unknown
02:48
created

Status::fromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\ValueObject;
4
5
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidStatusException;
6
7
final class Status
8
{
9
    const ACTIVE = 'active';
10
    const BLOCKED = 'blocked';
11
12
    /** @var string */
13
    private $status;
14
15
    /**
16
     * @param string $status
17
     *
18
     * @throws InvalidStatusException
19
     */
20
    private function __construct($status)
21
    {
22
        if (!in_array($status, self::getPossibleStatuses())) {
23
            throw InvalidStatusException::withStatus($status);
24
        }
25
26
        $this->status = $status;
27
    }
28
29
    public static function fromString($status)
30
    {
31
        return new self($status);
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public static function getPossibleStatuses()
38
    {
39
        $statuses = [
40
            self::ACTIVE,
41
            self::BLOCKED,
42
        ];
43
44
        return array_combine($statuses, $statuses);
45
    }
46
47
    /**
48
     * @return Status
49
     */
50
    public static function active()
51
    {
52
        return new self(self::ACTIVE);
53
    }
54
55
    /**
56
     * @return Status
57
     */
58
    public static function blocked()
59
    {
60
        return new self(self::BLOCKED);
61
    }
62
63
    /**
64
     * @return bool
65
     */
66
    public function isBlocked()
67
    {
68
        return $this->status === self::blocked();
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function __toString()
75
    {
76
        return $this->status;
77
    }
78
}
79