LoopCreatorClassProvider::getCreatorClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Loop;
6
7
use AlecRabbit\Spinner\Contract\ICreator;
8
use AlecRabbit\Spinner\Core\Loop\Contract\ILoopCreator;
9
use AlecRabbit\Spinner\Core\Loop\Contract\ILoopCreatorClassProvider;
10
use AlecRabbit\Spinner\Exception\InvalidArgument;
11
12
use function is_a;
13
use function sprintf;
14
15
final class LoopCreatorClassProvider implements ILoopCreatorClassProvider
16
{
17
    /** @var class-string<ILoopCreator>|null */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ILoopCreator>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ILoopCreator>|null.
Loading history...
18
    private ?string $creatorClass = null;
19
20
    /**
21
     * @param class-string<ICreator>|null $creatorClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ICreator>|null at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ICreator>|null.
Loading history...
22
     *
23
     * @throws InvalidArgument
24
     */
25
    public function __construct(?string $creatorClass)
26
    {
27
        self::assertClass($creatorClass);
28
        /** @var class-string<ILoopCreator>|null $creatorClass */
29
        $this->creatorClass = $creatorClass;
30
    }
31
32
    private static function assertClass(?string $creatorClass): void
33
    {
34
        if ($creatorClass === null) {
35
            return;
36
        }
37
        if (!is_a($creatorClass, ILoopCreator::class, true)) {
38
            throw new InvalidArgument(
39
                sprintf(
40
                    'Creator class must be of "%s" interface.',
41
                    ILoopCreator::class
42
                )
43
            );
44
        }
45
    }
46
47
    public function getCreatorClass(): ?string
48
    {
49
        return $this->creatorClass;
50
    }
51
}
52