ActionState::isFinished()   A
last analyzed

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 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
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
    const STATE_NEW      = 'new';
21
    const STATE_FINISHED = 'finished';
22
    const STATE_FAILED   = 'failed';
23
24
    /** @var string */
25
    protected $state;
26
27
    private function __construct(string $state = self::STATE_NEW)
28
    {
29
        $this->state = $state;
30
    }
31
32
    public function getName()
33
    {
34
        return $this->state;
35
    }
36
37
    public function isNew()
38
    {
39
        return $this->state === self::STATE_NEW;
40
    }
41
42
    public function isFinished()
43
    {
44
        return $this->state === self::STATE_FINISHED;
45
    }
46
47
    public static function new()
48
    {
49
        return new self(self::STATE_NEW);
50
    }
51
52
    public static function finished()
53
    {
54
        return new self(self::STATE_FINISHED);
55
    }
56
57
    public static function failed()
58
    {
59
        return new self(self::STATE_FAILED);
60
    }
61
62
    public static function fromString(string $name)
63
    {
64
        foreach ([self::STATE_NEW, self::STATE_FINISHED, self::STATE_FAILED] as $state) {
65
            if ($state === $name) {
66
                return new self($state);
67
            }
68
        }
69
70
        throw new \Exception("wrong action state '$name'");
71
    }
72
}
73