ChargeState::getName()   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\charge;
12
13
/**
14
 * Charge State.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class ChargeState
19
{
20
    const STATE_NEW      = 'new';
21
    const STATE_FINISHED = 'finished';
22
23
    /** @var string */
24
    protected $state;
25
26
    private function __construct(string $state = self::STATE_NEW)
27
    {
28
        $this->state = $state;
29
    }
30
31
    public function getName()
32
    {
33
        return $this->state;
34
    }
35
36
    public function isNew()
37
    {
38
        return $this->state === self::STATE_NEW;
39
    }
40
41
    public function isFinished()
42
    {
43
        return $this->state === self::STATE_FINISHED;
44
    }
45
46
    public static function new()
47
    {
48
        return new self(self::STATE_NEW);
49
    }
50
51
    public static function finished()
52
    {
53
        return new self(self::STATE_FINISHED);
54
    }
55
56
    public static function fromString(string $name)
57
    {
58
        foreach ([self::STATE_NEW, self::STATE_FINISHED] as $state) {
59
            if ($state === $name) {
60
                return new self($state);
61
            }
62
        }
63
64
        throw new \Exception("wrong charge state '$name'");
65
    }
66
}
67