Passed
Pull Request — main (#22)
by Andrey
20:42 queued 05:40
created

BaseFacade::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
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
    public static function __callStatic($method, $args)
12
    {
13
        $instance = static::getFacadeRoot();
14
15
        if (! $instance) {
16
            throw new RuntimeException('A facade root has not been set.');
17
        }
18
19
        return $instance->$method(...$args);
20
    }
21
22
    public static function getFacadeRoot()
23
    {
24
        return static::resolveFacadeInstance(static::getFacadeAccessor());
25
    }
26
27
    /**
28
     * Clear all of the resolved instances.
29
     */
30
    public static function clearResolvedInstances()
31
    {
32
        static::$resolved_instance = [];
33
    }
34
35
    /**
36
     * @return object|string
37
     */
38
    protected static function getFacadeAccessor()
39
    {
40
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
41
    }
42
43
    /**
44
     * @param  object|string  $facade
45
     *
46
     * @return object
47
     */
48
    protected static function resolveFacadeInstance($facade): object
49
    {
50
        if (is_object($facade)) {
51
            return $facade;
52
        }
53
        if (isset(static::$resolved_instance[$facade])) {
54
            return static::$resolved_instance[$facade];
55
        }
56
57
        return static::$resolved_instance[$facade] = new $facade();
58
    }
59
}
60