Progress   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 90
ccs 16
cts 16
cp 1
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 3 2
A current() 0 3 1
A total() 0 3 1
A setCurrent() 0 5 1
A percentage() 0 3 2
A setTotal() 0 5 1
A fraction() 0 3 2
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