Passed
Pull Request — main (#123)
by Andrey
12:00
created

Facade::getFacadeAccessor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

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
rs 10
1
<?php
2
3
namespace Helldar\Support\Facades;
4
5
use RuntimeException;
6
7
abstract class Facade
8
{
9
    protected static $resolved = [];
10
11
    public static function __callStatic($method, $args)
12
    {
13
        if ($instance = self::getFacadeRoot()) {
14
            return $instance->$method(...$args);
15
        }
16
17
        throw new RuntimeException('A facade root has not been set.');
18
    }
19
20
    public static function getFacadeRoot(): object
21
    {
22
        return self::resolveFacadeInstance(static::getFacadeAccessor());
23
    }
24
25
    public static function clearResolvedInstances()
26
    {
27
        self::$resolved = [];
28
    }
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
    protected static function resolveFacadeInstance($facade): object
44
    {
45
        $class = is_object($facade) ? get_class($facade) : $facade;
46
47
        if (isset(self::$resolved[$class])) {
48
            return self::$resolved[$class];
49
        }
50
51
        return self::$resolved[$class] = is_object($facade) ? $facade : new $facade();
52
    }
53
}
54