Credit::blank()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\ChargeableApi;
6
7
final class Credit
8
{
9
    private $value;
10
11
    public static function blank(): self
12
    {
13
        return self::fromInteger(0);
14
    }
15
16
    public static function fromInteger(int $value): self
17
    {
18
        return new self($value);
19
    }
20
21
    public function add(self $credit): self
22
    {
23
        return new self($this->value + $credit->value);
24
    }
25
26
    /**
27
     * @throws InvalidOperation
28
     */
29
    public function subtract(self $credit): self
30
    {
31
        if ($credit->value > $this->value) {
32
            throw InsufficientFunds::notEnough($credit->value - $this->value);
33
        }
34
35
        return new self($this->value - $credit->value);
36
    }
37
38
    public function toInteger(): int
39
    {
40
        return $this->value;
41
    }
42
43
    /**
44
     * @throws InvalidOperation
45
     */
46
    private function __construct(int $value)
47
    {
48
        if ($value < 0) {
49
            throw InvalidOperation::negativeCredit();
50
        }
51
52
        $this->value = $value;
53
    }
54
}
55