Passed
Pull Request — master (#73)
by
unknown
10:24
created

ActionState   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 13
eloc 26
c 4
b 0
f 2
dl 0
loc 77
ccs 0
cts 37
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A premature() 0 3 1
A getName() 0 3 1
A canceled() 0 3 1
A finished() 0 3 1
A fromString() 0 16 3
A future() 0 3 1
A equals() 0 3 1
A new() 0 3 1
A isNotActive() 0 3 1
A __construct() 0 2 1
A isNew() 0 3 1
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\action;
12
13
/**
14
 * Action State.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class ActionState
19
{
20
    private const STATE_NEW       = 'new';
21
22
    private const STATE_FINISHED  = 'finished';
23
24
    private const STATE_PREMATURE = 'premature';
25
26
    private const STATE_FUTURE    = 'future';
27
28
    private const STATE_CANCELED  = 'canceled';
29
30
    private function __construct(protected string $state = self::STATE_NEW)
31
    {
32
    }
33
34
    public function getName(): string
35
    {
36
        return $this->state;
37
    }
38
39
    public function isNew(): bool
40
    {
41
        return $this->state === self::STATE_NEW;
42
    }
43
44
    public function isNotActive(): bool
45
    {
46
        return !$this->isNew();
47
    }
48
49
    public static function new(): self
50
    {
51
        return new self(self::STATE_NEW);
52
    }
53
54
    public static function finished(): self
55
    {
56
        return new self(self::STATE_FINISHED);
57
    }
58
59
    public static function premature(): self
60
    {
61
        return new self(self::STATE_PREMATURE);
62
    }
63
64
    public static function future(): self
65
    {
66
        return new self(self::STATE_FUTURE);
67
    }
68
69
    public static function canceled(): self
70
    {
71
        return new self(self::STATE_CANCELED);
72
    }
73
74
    public static function fromString(string $name): self
75
    {
76
        $allowedStates = [
77
            self::STATE_NEW,
78
            self::STATE_FINISHED,
79
            self::STATE_PREMATURE,
80
            self::STATE_FUTURE,
81
            self::STATE_CANCELED,
82
        ];
83
        foreach ($allowedStates as $state) {
84
            if ($state === $name) {
85
                return new self($state);
86
            }
87
        }
88
89
        throw new \Exception("wrong action state '$name'");
90
    }
91
92
    public function equals(ActionState $other): bool
93
    {
94
        return $this->state === $other->getName();
95
    }
96
}
97