Passed
Push — main ( 53ac6e...e37cca )
by Chema
01:30 queued 15s
created

AbstractFacade::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
8
use RuntimeException;
9
10
/**
11
 * The `__callStatic` and `__call` methods allow defining `getFactory` as static and non-static methods.
12
 *
13
 * @psalm-suppress MethodSignatureMismatch
14
 *
15
 * @method static AbstractFactory getFactory()
16
 * @method AbstractFactory getFactory()
17
 */
18
abstract class AbstractFacade
19
{
20
    /** @var array<string, AbstractFactory> */
21
    private static array $factories = [];
22
23
    /**
24
     * @param list<mixed> $arguments
25
     */
26
    public static function __callStatic(string $name = '', array $arguments = [])
27
    {
28
        if ($name === 'getFactory') {
29
            return self::doGetFactory();
30
        }
31
32
        throw new RuntimeException(sprintf("Method unknown: '%s'", $name));
33
    }
34
35
    /**
36
     * @param list<mixed> $arguments
37
     */
38
    public function __call(string $name = '', array $arguments = [])
39
    {
40
        if ($name === 'getFactory') {
41
            return self::doGetFactory();
42
        }
43
44
        throw new RuntimeException(sprintf("Method unknown: '%s'", $name));
45
    }
46
47
    public static function resetCache(): void
48
    {
49
        self::$factories = [];
50
    }
51
52
    private static function doGetFactory(): AbstractFactory
53
    {
54
        return self::$factories[static::class]
55
            ??= (new FactoryResolver())->resolve(static::class);
56
    }
57
}
58