Passed
Pull Request — master (#73)
by
unknown
21:53 queued 06:53
created

CustomerState::blocked()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace hiqdev\php\billing\customer;
4
5
class CustomerState
6
{
7
    public const BLOCKED = 'blocked';
8
9
    public const DELETED = 'deleted';
10
11
    public const NEW = 'new';
12
13
    public const OK = 'ok';
14
15
    private function __construct(protected string $state = self::NEW)
16
    {
17
    }
18
19
    public function getName(): string
20
    {
21
        return $this->state;
22
    }
23
24
    public static function isDeleted(CustomerInterface $customer): bool
25
    {
26
        return $customer->getState() === self::DELETED;
27
    }
28
29
    public static function deleted(): CustomerState
30
    {
31
        return new self(self::DELETED);
32
    }
33
34
    public static function blocked(): CustomerState
35
    {
36
        return new self(self::BLOCKED);
37
    }
38
39
    public static function new(): CustomerState
40
    {
41
        return new self(self::NEW);
42
    }
43
44
    public static function ok(): CustomerState
45
    {
46
        return new self(self::OK);
47
    }
48
49
    public static function fromString(string $name): self
50
    {
51
        $allowedStates = [
52
            self::BLOCKED,
53
            self::DELETED,
54
            self::NEW,
55
            self::OK,
56
        ];
57
        foreach ($allowedStates as $state) {
58
            if ($state === $name) {
59
                return new self($state);
60
            }
61
        }
62
63
        throw new \Exception("wrong customer state '$name'");
64
    }
65
}
66