SyncProgress   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 24
cts 29
cp 0.8276

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A step() 0 5 1
A callProgressCallback() 0 6 2
B assertValidArguments() 0 18 6
1
<?php
2
3
namespace HansOtt\Lastify;
4
5
use InvalidArgumentException;
6
7
final class SyncProgress
8
{
9
    private $current;
10
11
    private $total;
12
13
    private $callback = null;
14
15 6
    public function __construct($initial = 0, $total, SyncProgressCallback $callback = null)
16
    {
17 6
        $this->assertValidArguments($initial, $total);
18 6
        $this->current = (int) $initial;
19 6
        $this->total = (int) $total;
20
21 6
        if (isset($callback)) {
22 3
            $this->callback = $callback;
23 3
        }
24 6
    }
25
26 3
    public function step(TrackInfo $currentItem)
27
    {
28 3
        $this->current++;
29 3
        $this->callProgressCallback($currentItem);
30 3
    }
31
32 3
    private function callProgressCallback($currentItem)
33
    {
34 3
        if ($this->callback instanceof SyncProgressCallback) {
35 3
            $this->callback->onProgress($this->current, $this->total, $currentItem);
36 3
        }
37 3
    }
38
39 6
    private function assertValidArguments($initial, $total)
40
    {
41 6
        if (!is_int($initial)) {
42
            throw new InvalidArgumentException('The initial value should be an integer, instead got:' . gettype($initial));
43
        }
44 6
        if ($initial < 0) {
45
            throw new InvalidArgumentException('The initial value should be a positive integer');
46
        }
47 6
        if (!is_int($total)) {
48
            throw new InvalidArgumentException('The total value should be an integer, instead got:' . gettype($total));
49
        }
50 6
        if ($total < 0) {
51
            throw new InvalidArgumentException('The total value should be a positive integer');
52
        }
53 6
        if ($total <= $initial) {
54
            throw new InvalidArgumentException('The total value should be a greater than the initial value.');
55
        }
56 6
    }
57
}
58