Progress::total()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\JsonParser\ValueObjects;
4
5
use function is_null;
6
7
/**
8
 * The parsing progress.
9
 *
10
 */
11
final class Progress
12
{
13
    /**
14
     * The current progress.
15
     *
16
     * @var int
17
     */
18
    private int $current = 0;
19
20
    /**
21
     * The total possible progress.
22
     *
23
     * @var int|null
24
     */
25
    private ?int $total = null;
26
27
    /**
28
     * Set the current progress
29
     *
30
     * @param int $current
31
     * @return self
32
     */
33 2
    public function setCurrent(int $current): self
34
    {
35 2
        $this->current = $current;
36
37 2
        return $this;
38
    }
39
40
    /**
41
     * Retrieve the current progress
42
     *
43
     * @return int
44
     */
45 1
    public function current(): int
46
    {
47 1
        return $this->current;
48
    }
49
50
    /**
51
     * Set the total possible progress
52
     *
53
     * @param int|null $total
54
     * @return self
55
     */
56 2
    public function setTotal(?int $total): self
57
    {
58 2
        $this->total ??= $total;
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * Retrieve the total possible progress
65
     *
66
     * @return int|null
67
     */
68 1
    public function total(): ?int
69
    {
70 1
        return $this->total;
71
    }
72
73
    /**
74
     * Retrieve the formatted percentage of the progress
75
     *
76
     * @return string|null
77
     */
78 1
    public function format(): ?string
79
    {
80 1
        return is_null($percentage = $this->percentage()) ? null : number_format($percentage, 1) . '%';
81
    }
82
83
    /**
84
     * Retrieve the percentage of the progress
85
     *
86
     * @return float|null
87
     */
88 2
    public function percentage(): ?float
89
    {
90 2
        return is_null($fraction = $this->fraction()) ? null : $fraction * 100;
91
    }
92
93
    /**
94
     * Retrieve the fraction of the progress
95
     *
96
     * @return float|null
97
     */
98 2
    public function fraction(): ?float
99
    {
100 2
        return $this->total ? $this->current / $this->total : null;
101
    }
102
}
103