Passed
Pull Request — main (#22)
by Andrey
29:13 queued 14:13
created

BaseFacade::resolveFacadeInstance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Helldar\Support\Facades;
4
5
use RuntimeException;
6
7
abstract class BaseFacade
8
{
9
    protected static $resolvedInstance = [];
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
     * @return object|string
29
     */
30
    protected static function getFacadeAccessor()
31
    {
32
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
33
    }
34
35
    /**
36
     * @param  object|string  $facade
37
     *
38
     * @return object
39
     */
40
    protected static function resolveFacadeInstance($facade): object
41
    {
42
        if (is_object($facade)) {
43
            return $facade;
44
        }
45
        if (isset(static::$resolvedInstance[$facade])) {
46
            return static::$resolvedInstance[$facade];
47
        }
48
49
        return static::$resolvedInstance[$facade] = new $facade();
50
    }
51
}
52