Passed
Push — master ( 823aee...b9b37f )
by Alec
13:15 queued 12s
created

DefaultsFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 25
c 5
b 0
f 0
dl 0
loc 71
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 2
A addProbe() 0 12 3
A get() 0 6 2
A setDefaultsClass() 0 11 2
A create() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
// 14.02.23
5
6
namespace AlecRabbit\Spinner\Core\Factory;
7
8
use AlecRabbit\Spinner\Contract\IProbe;
9
use AlecRabbit\Spinner\Core\Defaults\A\ADefaults;
10
use AlecRabbit\Spinner\Core\Defaults\Contract\IDefaults;
11
use AlecRabbit\Spinner\Exception\DomainException;
12
use AlecRabbit\Spinner\Exception\InvalidArgumentException;
13
use AlecRabbit\Spinner\Helper\Asserter;
14
use AlecRabbit\Spinner\Mixin\NoInstanceTrait;
15
16
final class DefaultsFactory
17
{
18
    use NoInstanceTrait;
19
20
    /** @var array<class-string<IProbe>> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string<IProbe>> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string<IProbe>>.
Loading history...
21
    private static iterable $addedProbes = [];
22
23
    /** @var null|class-string<IDefaults> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment null|class-string<IDefaults> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in null|class-string<IDefaults>.
Loading history...
24
    private static ?string $className = null;
25
    private static ?IDefaults $defaultsInstance = null;
26
27
    public static function get(): IDefaults
28
    {
29
        if (null === self::$defaultsInstance) {
30
            self::$defaultsInstance = self::create();
31
        }
32
        return self::$defaultsInstance;
33
    }
34
35
    private static function create(): IDefaults
36
    {
37
        if (null === self::$className) {
38
            self::$className = ADefaults::class;
39
        }
40
        self::initialize(self::$className);
41
        /** @noinspection PhpUndefinedMethodInspection */
42
        return self::$className::getInstance();
43
    }
44
45
    private static function initialize(string $className): void
46
    {
47
        /** @var IDefaults $className */
48
        foreach (self::$addedProbes as $probe) {
49
            $className::registerProbeClass($probe);
50
        }
51
    }
52
53
    /**
54
     * @param class-string<IProbe> $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<IProbe> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<IProbe>.
Loading history...
55
     * @throws InvalidArgumentException
56
     */
57
    public static function addProbe(string $className): void
58
    {
59
        Asserter::assertClassExists($className, __METHOD__);
60
61
        foreach (self::$addedProbes as $probe) {
62
            if ($probe === $className) {
63
                throw new InvalidArgumentException(
64
                    sprintf('Probe class "%s" is already added.', $className)
65
                );
66
            }
67
        }
68
        self::$addedProbes[] = $className;
69
    }
70
71
    /**
72
     * @param class-string<IDefaults> $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<IDefaults> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<IDefaults>.
Loading history...
73
     * @throws InvalidArgumentException
74
     * @throws DomainException
75
     */
76
    public static function setDefaultsClass(string $class): void
77
    {
78
        Asserter::isSubClass($class, IDefaults::class, __METHOD__);
79
80
        if (null !== self::$defaultsInstance) {
81
            throw new DomainException(
82
                'Defaults class can not be set after defaults instance is created.'
83
            );
84
        }
85
86
        self::$className = $class;
87
    }
88
}
89