ActionState   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 17
c 2
b 0
f 2
dl 0
loc 53
ccs 0
cts 36
cp 0
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A finished() 0 3 1
A fromString() 0 9 3
A failed() 0 3 1
A new() 0 3 1
A __construct() 0 3 1
A isFinished() 0 3 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
    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