Passed
Pull Request — master (#73)
by
unknown
16:36 queued 01:36
created

ActionState::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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_FAILED    = 'failed';
25
26
    private const STATE_PREMATURE = 'premature';
27
28
    private const STATE_FUTURE    = 'future';
29
30
    private const STATE_CANCELED  = 'canceled';
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 isNotActive(): 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
    public static function finished(): self
57
    {
58
        return new self(self::STATE_FINISHED);
59
    }
60
61
    public static function failed(): self
62
    {
63
        return new self(self::STATE_FAILED);
64
    }
65
66
    public static function premature(): self
67
    {
68
        return new self(self::STATE_PREMATURE);
69
    }
70
71
    public static function future(): self
72
    {
73
        return new self(self::STATE_FUTURE);
74
    }
75
76
    public static function canceled(): self
77
    {
78
        return new self(self::STATE_CANCELED);
79
    }
80
81
    public static function fromString(string $name): self
82
    {
83
        $allowedStates = [
84
            self::STATE_NEW,
85
            self::STATE_FINISHED,
86
            self::STATE_FAILED,
87
            self::STATE_PREMATURE,
88
            self::STATE_FUTURE,
89
            self::STATE_CANCELED,
90
        ];
91
        foreach ($allowedStates as $state) {
92
            if ($state === $name) {
93
                return new self($state);
94
            }
95
        }
96
97
        throw new \Exception("wrong action state '$name'");
98
    }
99
100
    public function equals(ActionState $other): bool
101
    {
102
        return $this->state === $other->getName();
103
    }
104
}
105