Passed
Push — add-with-all-to-modules-list ( 82b81d...0edb38 )
by Chema
03:40
created

FactoryResolverAwareTrait::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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
trait FactoryResolverAwareTrait
19
{
20
    /** @var array<string, AbstractFactory> */
21
    private static array $factories = [];
22
23 4
    public static function __callStatic(string $name = '', array $arguments = [])
24
    {
25 4
        if ($name === 'getFactory') {
26 2
            return self::doGetFactory();
27
        }
28
29 2
        throw new RuntimeException("Method unknown: '{$name}'");
30
    }
31
32 63
    public function __call(string $name = '', array $arguments = [])
33
    {
34 63
        if ($name === 'getFactory') {
35 62
            return self::doGetFactory();
36
        }
37
38 1
        throw new RuntimeException("Method unknown: '{$name}'");
39
    }
40
41 59
    public static function resetCache(): void
42
    {
43 59
        self::$factories = [];
44
    }
45
46 64
    private static function doGetFactory(): AbstractFactory
47
    {
48 64
        return self::$factories[static::class]
49 64
            ??= (new FactoryResolver())->resolve(static::class);
50
    }
51
}
52