Passed
Push — main ( 7dacd3...16ab53 )
by Andrey
39:40 queued 38:12
created

BaseFacade::getFacadeRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 = static::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 static::resolveFacadeInstance(static::getFacadeAccessor());
23
    }
24
25
    /**
26
     * Clear all of the resolved instances.
27
     */
28 261
    public static function clearResolvedInstances()
29
    {
30 261
        static::$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
        if (is_object($facade)) {
49
            return $facade;
50
        }
51
52 261
        if (isset(static::$resolved_instance[$facade])) {
53 149
            return static::$resolved_instance[$facade];
54
        }
55
56 261
        return static::$resolved_instance[$facade] = new $facade();
57
    }
58
}
59