LoopCreatorClassExtractor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A assertProbe() 0 7 2
A extract() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Loop;
6
7
use AlecRabbit\Spinner\Contract\Probe\IStaticProbe;
8
use AlecRabbit\Spinner\Core\Loop\Contract\ILoopCreatorClassExtractor;
9
use AlecRabbit\Spinner\Core\Loop\Contract\ILoopProbe;
10
use AlecRabbit\Spinner\Exception\InvalidArgument;
11
use Traversable;
12
13
final class LoopCreatorClassExtractor implements ILoopCreatorClassExtractor
14
{
15
    public function extract(Traversable $probes): ?string
16
    {
17
        /** @var IStaticProbe $probe */
18
        foreach ($probes as $probe) {
19
            self::assertProbe($probe);
20
            if ($probe::isSupported()) {
21
                return $probe::getCreatorClass();
22
            }
23
        }
24
        return null;
25
    }
26
27
    private static function assertProbe(mixed $probe): void
28
    {
29
        if (!is_a($probe, ILoopProbe::class, true)) {
30
            throw new InvalidArgument(
31
                sprintf(
32
                    'Probe must be an instance of "%s" interface.',
33
                    ILoopProbe::class
34
                )
35
            );
36
        }
37
    }
38
}
39