Passed
Pull Request — master (#73)
by
unknown
12:15
created

CustomerState   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 10
eloc 21
c 5
b 1
f 2
dl 0
loc 59
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A isDeleted() 0 3 1
A deleted() 0 3 1
A __construct() 0 2 1
A new() 0 3 1
A blocked() 0 3 1
A fromString() 0 15 3
A ok() 0 3 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()?->getName() === 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