Passed
Push — master ( 45b34a...9eb8da )
by
unknown
01:50
created

Facade::getMockableClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CarbonFramework\Facades;
4
5
use Mockery;
6
use RuntimeException;
7
use Mockery\MockInterface;
8
9
/**
10
 * Base facade class that should be extended by all facades
11
 * @credit illuminate/support
12
 */
13
abstract class Facade
14
{
15
    /**
16
     * The application instance being facaded.
17
     *
18
     * @var object
19
     */
20
    protected static $app;
21
22
    /**
23
     * The resolved object instances.
24
     *
25
     * @var array
26
     */
27
    protected static $resolvedInstance;
28
29
    /**
30
     * Convert the facade into a Mockery spy.
31
     *
32
     * @return void
33
     */
34
    public static function spy()
35
    {
36
        if (! static::isMock()) {
37
            $class = static::getMockableClass();
38
39
            static::swap($class ? Mockery::spy($class) : Mockery::spy());
40
        }
41
    }
42
43
    /**
44
     * Initiate a mock expectation on the facade.
45
     *
46
     * @return \Mockery\Expectation
47
     */
48
    public static function shouldReceive()
49
    {
50
        $name = static::getFacadeAccessor();
51
52
        $mock = static::isMock()
53
                    ? static::$resolvedInstance[$name]
54
                    : static::createFreshMockInstance();
55
56
        return call_user_func_array([$mock, 'shouldReceive'], func_get_args());
57
    }
58
59
    /**
60
     * Create a fresh mock instance for the given class.
61
     *
62
     * @return \Mockery\Expectation
63
     */
64
    protected static function createFreshMockInstance()
65
    {
66
        return tap(static::createMock(), function ($mock) {
67
            static::swap($mock);
68
69
            $mock->shouldAllowMockingProtectedMethods();
70
        });
71
    }
72
73
    /**
74
     * Create a fresh mock instance for the given class.
75
     *
76
     * @return \Mockery\MockInterface
77
     */
78
    protected static function createMock()
79
    {
80
        $class = static::getMockableClass();
81
82
        return $class ? Mockery::mock($class) : Mockery::mock();
83
    }
84
85
    /**
86
     * Determines whether a mock is set as the instance of the facade.
87
     *
88
     * @return bool
89
     */
90
    protected static function isMock()
91
    {
92
        $name = static::getFacadeAccessor();
93
94
        return isset(static::$resolvedInstance[$name]) &&
95
               static::$resolvedInstance[$name] instanceof MockInterface;
96
    }
97
98
    /**
99
     * Get the mockable class for the bound instance.
100
     *
101
     * @return string|null
102
     */
103
    protected static function getMockableClass()
104
    {
105
        if ($root = static::getFacadeRoot()) {
106
            return get_class($root);
107
        }
108
    }
109
110
    /**
111
     * Hotswap the underlying instance behind the facade.
112
     *
113
     * @param  mixed  $instance
114
     * @return void
115
     */
116
    public static function swap($instance)
117
    {
118
        static::$resolvedInstance[static::getFacadeAccessor()] = $instance;
119
120
        if (isset(static::$app)) {
121
            static::$app->instance(static::getFacadeAccessor(), $instance);
122
        }
123
    }
124
125
    /**
126
     * Get the root object behind the facade.
127
     *
128
     * @return mixed
129
     */
130
    public static function getFacadeRoot()
131
    {
132
        return static::resolveFacadeInstance(static::getFacadeAccessor());
133
    }
134
135
    /**
136
     * Get the registered name of the component.
137
     *
138
     * @return string
139
     *
140
     * @throws \RuntimeException
141
     */
142
    protected static function getFacadeAccessor()
143
    {
144
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
145
    }
146
147
    /**
148
     * Resolve the facade root instance from the container.
149
     *
150
     * @param  string|object  $name
151
     * @return mixed
152
     */
153
    protected static function resolveFacadeInstance($name)
154
    {
155
        if (is_object($name)) {
156
            return $name;
157
        }
158
159
        if (isset(static::$resolvedInstance[$name])) {
160
            return static::$resolvedInstance[$name];
161
        }
162
163
        return static::$resolvedInstance[$name] = static::$app[$name];
164
    }
165
166
    /**
167
     * Clear a resolved facade instance.
168
     *
169
     * @param  string  $name
170
     * @return void
171
     */
172
    public static function clearResolvedInstance($name)
173
    {
174
        unset(static::$resolvedInstance[$name]);
175
    }
176
177
    /**
178
     * Clear all of the resolved instances.
179
     *
180
     * @return void
181
     */
182
    public static function clearResolvedInstances()
183
    {
184
        static::$resolvedInstance = [];
185
    }
186
187
    /**
188
     * Get the application instance behind the facade.
189
     *
190
     * @return object
191
     */
192
    public static function getFacadeApplication()
193
    {
194
        return static::$app;
195
    }
196
197
    /**
198
     * Set the application instance.
199
     *
200
     * @param  object $app
201
     * @return void
202
     */
203
    public static function setFacadeApplication($app)
204
    {
205
        static::$app = $app;
206
    }
207
208
    /**
209
     * Handle dynamic, static calls to the object.
210
     *
211
     * @param  string  $method
212
     * @param  array   $args
213
     * @return mixed
214
     *
215
     * @throws \RuntimeException
216
     */
217
    public static function __callStatic($method, $args)
218
    {
219
        $instance = static::getFacadeRoot();
220
221
        if (! $instance) {
222
            throw new RuntimeException('A facade root has not been set.');
223
        }
224
225
        return call_user_func_array([$instance, $method], $args);
226
    }
227
}
228