FrameRevolverBuilder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 46
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withTolerance() 0 5 1
A withInterval() 0 5 1
A withFrameCollection() 0 5 1
A validate() 0 7 1
A build() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Revolver;
6
7
use AlecRabbit\Spinner\Contract\IInterval;
8
use AlecRabbit\Spinner\Core\Contract\IFrameCollection;
9
use AlecRabbit\Spinner\Core\Contract\ITolerance;
10
use AlecRabbit\Spinner\Core\Revolver\Contract\IFrameRevolver;
11
use AlecRabbit\Spinner\Core\Revolver\Contract\IFrameRevolverBuilder;
12
use AlecRabbit\Spinner\Exception\LogicException;
13
14
/**
15
 * @psalm-suppress PossiblyNullArgument
16
 */
17
final class FrameRevolverBuilder implements IFrameRevolverBuilder
18
{
19
    private ?IFrameCollection $frames = null;
20
    private ?IInterval $interval = null;
21
    private ?ITolerance $tolerance = null;
22
23
    public function build(): IFrameRevolver
24
    {
25
        $this->validate();
26
27
        return new FrameCollectionRevolver(
28
            $this->frames,
0 ignored issues
show
Bug introduced by
It seems like $this->frames can also be of type null; however, parameter $frameCollection of AlecRabbit\Spinner\Core\...Revolver::__construct() does only seem to accept AlecRabbit\Spinner\Core\Contract\IFrameCollection, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            /** @scrutinizer ignore-type */ $this->frames,
Loading history...
29
            $this->interval,
0 ignored issues
show
Bug introduced by
It seems like $this->interval can also be of type null; however, parameter $interval of AlecRabbit\Spinner\Core\...Revolver::__construct() does only seem to accept AlecRabbit\Spinner\Contract\IInterval, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
            /** @scrutinizer ignore-type */ $this->interval,
Loading history...
30
            $this->tolerance,
0 ignored issues
show
Bug introduced by
It seems like $this->tolerance can also be of type null; however, parameter $tolerance of AlecRabbit\Spinner\Core\...Revolver::__construct() does only seem to accept AlecRabbit\Spinner\Core\Contract\ITolerance, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            /** @scrutinizer ignore-type */ $this->tolerance,
Loading history...
31
        );
32
    }
33
34
    private function validate(): void
35
    {
36
        match (true) {
37
            $this->frames === null => throw new LogicException('Frame collection is not set.'),
38
            $this->interval === null => throw new LogicException('Interval is not set.'),
39
            $this->tolerance === null => throw new LogicException('Tolerance is not set.'),
40
            default => null,
41
        };
42
    }
43
44
    public function withFrameCollection(IFrameCollection $frames): IFrameRevolverBuilder
45
    {
46
        $clone = clone $this;
47
        $clone->frames = $frames;
48
        return $clone;
49
    }
50
51
    public function withInterval(IInterval $interval): IFrameRevolverBuilder
52
    {
53
        $clone = clone $this;
54
        $clone->interval = $interval;
55
        return $clone;
56
    }
57
58
    public function withTolerance(ITolerance $tolerance): IFrameRevolverBuilder
59
    {
60
        $clone = clone $this;
61
        $clone->tolerance = $tolerance;
62
        return $clone;
63
    }
64
}
65