Credit   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 46
c 0
b 0
f 0
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A subtract() 0 7 2
A fromInteger() 0 3 1
A toInteger() 0 3 1
A __construct() 0 7 2
A add() 0 3 1
A blank() 0 3 1
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