Passed
Pull Request — master (#73)
by
unknown
11:25
created

CustomerState::fromString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 9
c 1
b 1
f 0
dl 0
loc 15
rs 9.9666
cc 3
nc 3
nop 1
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 fromString(string $name): self
35
    {
36
        $allowedStates = [
37
            self::BLOCKED,
38
            self::DELETED,
39
            self::NEW,
40
            self::OK,
41
        ];
42
        foreach ($allowedStates as $state) {
43
            if ($state === $name) {
44
                return new self($state);
45
            }
46
        }
47
48
        throw new \Exception("wrong customer state '$name'");
49
    }
50
}
51