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

FactoryResolverAwareTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 11
c 6
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 8 2
A resetCache() 0 3 1
A doGetFactory() 0 5 1
A __call() 0 8 2
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