Passed
Push — factory-and-get-factory ( 652c17...af38f3 )
by Jesús
12:34 queued 11:54
created

FactoryResolverAwareTrait::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework;
6
7
use Gacela\Framework\ClassResolver\Factory\FactoryResolver;
8
9
/**
10
 * @psalm-suppress MethodSignatureMismatch
11
 *
12
 * @method AbstractFactory getFactory()
13
 */
14
trait FactoryResolverAwareTrait
15
{
16
    /** @var array<string,AbstractFactory> */
17
    private static array $factories = [];
18
19
    public static function __callStatic(string $name = '', array $arguments = [])
20
    {
21
        if ($name === 'getFactory') {
22
            return self::doGetFactory();
23
        }
24
25
        /** @psalm-suppress ParentNotFound */
26
        return parent::__callStatic($name, $arguments);
27
    }
28
29
    public function __call(string $name = '', array $arguments = [])
30
    {
31
        if ($name === 'getFactory') {
32
            return self::doGetFactory();
33
        }
34
35
        /** @psalm-suppress ParentNotFound */
36
        return parent::__call($name, $arguments);
37
    }
38
39
    public static function resetCache(): void
40
    {
41
        self::$factories = [];
42
    }
43
44
    private static function doGetFactory(): AbstractFactory
45
    {
46
        self::$factories[static::class] ??= (new FactoryResolver())->resolve(static::class);
47
48
        return self::$factories[static::class];
49
    }
50
}
51