Passed
Push — bugfix/support-windows ( 517fb4...0e99e7 )
by Chema
04:47
created

FactoryResolverAwareTrait::resetCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
 * @psalm-suppress MethodSignatureMismatch
12
 *
13
 * @method AbstractFactory getFactory()
14
 */
15
trait FactoryResolverAwareTrait
16
{
17
    /** @var array<string,AbstractFactory> */
18
    private static array $factories = [];
19
20 3
    public static function __callStatic(string $name = '', array $arguments = [])
21
    {
22 3
        if ($name === 'getFactory') {
23 2
            return self::doGetFactory();
24
        }
25
26 1
        if (method_exists(static::class, $name)) {
27
            /** @psalm-suppress ParentNotFound */
28
            return parent::__callStatic($name, $arguments);
29
        }
30
31 1
        throw new RuntimeException("Method unknown: '{$name}'");
32
    }
33
34 56
    public function __call(string $name = '', array $arguments = [])
35
    {
36 56
        if ($name === 'getFactory') {
37 56
            return self::doGetFactory();
38
        }
39
40
        if (method_exists(static::class, $name)) {
41
            /** @psalm-suppress ParentNotFound */
42
            return parent::__call($name, $arguments);
43
        }
44
45
        throw new RuntimeException("Method unknown: '{$name}'");
46
    }
47
48 45
    public static function resetCache(): void
49
    {
50 45
        self::$factories = [];
51
    }
52
53 58
    private static function doGetFactory(): AbstractFactory
54
    {
55 58
        return self::$factories[static::class]
56 58
            ??= (new FactoryResolver())->resolve(static::class);
57
    }
58
}
59