Passed
Pull Request — master (#73)
by
unknown
16:36 queued 01:36
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 new() 0 3 1
A blocked() 0 3 1
A deleted() 0 3 1
A fromString() 0 15 3
A ok() 0 3 1
A __construct() 0 2 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