Passed
Pull Request — master (#73)
by
unknown
13:55
created

CustomerState::isDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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