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

CustomerState   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 7
eloc 17
c 4
b 1
f 1
dl 0
loc 44
rs 10

5 Methods

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