Passed
Push — main ( 16ab53...91f55c )
by Andrey
01:26
created

BaseFacade   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 81.25%

Importance

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

5 Methods

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