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

BaseFacade   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveFacadeInstance() 0 10 3
A __callStatic() 0 9 2
A getFacadeAccessor() 0 3 1
A getFacadeRoot() 0 3 1
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