Passed
Push — master ( 200d88...9b81d7 )
by Alec
02:43 queued 15s
created

ALoopFactory::createLoopAdapter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Asynchronous\Factory\A;
6
7
use AlecRabbit\Spinner\Contract\IProbe;
8
use AlecRabbit\Spinner\Core\Contract\ILoopAdapter;
9
use AlecRabbit\Spinner\Core\Contract\ILoopProbe;
10
use AlecRabbit\Spinner\Core\Factory\A\ADefaultsAwareClass;
11
use AlecRabbit\Spinner\Core\Factory\Contract\ILoopFactory;
12
use AlecRabbit\Spinner\Core\Factory\DefaultsFactory;
13
use AlecRabbit\Spinner\Mixin\NoInstanceTrait;
14
use DomainException;
15
use Traversable;
16
17
abstract class ALoopFactory extends ADefaultsAwareClass implements ILoopFactory
18
{
19
    use NoInstanceTrait;
20
21
    protected static ?ILoopAdapter $loop = null;
22
23
    final public static function create(): ILoopAdapter
24
    {
25
        if (static::$loop instanceof ILoopAdapter) {
26
            return static::$loop;
27
        }
28
        return static::createLoopAdapter();
29
    }
30
31
    protected static function createLoopAdapter(): ILoopAdapter
32
    {
33
        /** @var class-string<IProbe> $probe */
34
        foreach (static::getProbeClasses() as $probe) {
35
            if (is_subclass_of($probe, ILoopProbe::class) && $probe::isSupported()) {
36
                return $probe::createLoop();
37
            }
38
        }
39
        throw new DomainException(
40
            'No supported event loop found.' .
41
            ' Check you have installed one of the supported event loops.' .
42
            ' Check your probes list if you have modified it.'
43
        );
44
    }
45
46
    protected static function getProbeClasses(): Traversable
47
    {
48
        return
49
            DefaultsFactory::get()->getProbeClasses();
50
    }
51
}
52