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

BaseFacade::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 2
b 1
f 0
nc 2
nop 2
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
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