Passed
Push — main ( a3c66a...a7b6ef )
by Andrey
12:42 queued 11:13
created

Facade   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 45
ccs 13
cts 16
cp 0.8125
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacadeAccessor() 0 3 1
A __callStatic() 0 7 2
A clearResolvedInstances() 0 3 1
A resolveFacadeInstance() 0 9 4
A getFacadeRoot() 0 3 1
1
<?php
2
3
namespace Helldar\Support\Facades;
4
5
use RuntimeException;
6
7
abstract class Facade
8
{
9
    protected static $resolved = [];
10
11 603
    public static function __callStatic($method, $args)
12
    {
13 603
        if ($instance = self::getFacadeRoot()) {
14 603
            return $instance->$method(...$args);
15
        }
16
17
        throw new RuntimeException('A facade root has not been set.');
18
    }
19
20 603
    public static function getFacadeRoot(): object
21
    {
22 603
        return self::resolveFacadeInstance(static::getFacadeAccessor());
23
    }
24
25 603
    public static function clearResolvedInstances()
26
    {
27 603
        self::$resolved = [];
28 603
    }
29
30
    /**
31
     * @return object|string
32
     */
33
    protected static function getFacadeAccessor()
34
    {
35
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
36
    }
37
38
    /**
39
     * @param  object|string  $facade
40
     *
41
     * @return object
42
     */
43 603
    protected static function resolveFacadeInstance($facade): object
44
    {
45 603
        $class = is_object($facade) ? get_class($facade) : $facade;
46
47 603
        if (isset(self::$resolved[$class])) {
48 432
            return self::$resolved[$class];
49
        }
50
51 603
        return self::$resolved[$class] = is_object($facade) ? $facade : new $facade();
52
    }
53
}
54