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

ActionState   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 13
eloc 28
c 3
b 0
f 2
dl 0
loc 84
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 17 3
A future() 0 3 1
A new() 0 3 1
A __construct() 0 2 1
A isFinished() 0 3 1
A isNew() 0 3 1
A expired() 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 const STATE_EXPIRED   = 'expired';
31
32
    private function __construct(protected string $state = self::STATE_NEW)
33
    {
34
    }
35
36
    public function getName(): string
37
    {
38
        return $this->state;
39
    }
40
41
    public function isNew(): bool
42
    {
43
        return $this->state === self::STATE_NEW;
44
    }
45
46
    public function isFinished(): bool
47
    {
48
        return !$this->isNew();
49
    }
50
51
    public static function new(): self
52
    {
53
        return new self(self::STATE_NEW);
54
    }
55
56
    /**
57
     * @deprecated use ActionState::expired()
58
     * @return self
59
     */
60
    public static function finished(): self
61
    {
62
        return new self(self::STATE_FINISHED);
63
    }
64
65
    public static function premature(): self
66
    {
67
        return new self(self::STATE_PREMATURE);
68
    }
69
70
    public static function future(): self
71
    {
72
        return new self(self::STATE_FUTURE);
73
    }
74
75
    public static function canceled(): self
76
    {
77
        return new self(self::STATE_CANCELED);
78
    }
79
80
    public static function expired(): self
81
    {
82
        return new self(self::STATE_EXPIRED);
83
    }
84
85
    public static function fromString(string $name): self
86
    {
87
        $allowedStates = [
88
            self::STATE_NEW,
89
            self::STATE_FINISHED,
90
            self::STATE_PREMATURE,
91
            self::STATE_FUTURE,
92
            self::STATE_CANCELED,
93
            self::STATE_EXPIRED,
94
        ];
95
        foreach ($allowedStates as $state) {
96
            if ($state === $name) {
97
                return new self($state);
98
            }
99
        }
100
101
        throw new \Exception("wrong action state '$name'");
102
    }
103
}
104