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

BaseFacade   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 13
c 2
b 1
f 0
dl 0
loc 50
ccs 13
cts 17
cp 0.7647
rs 10
wmc 8

5 Methods

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