ResetProviders   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rebindProviderContainer() 0 8 1
A handle() 0 13 4
1
<?php
2
3
namespace SwooleTW\Http\Server\Resetters;
4
5
use Illuminate\Contracts\Container\Container;
6
use SwooleTW\Http\Server\Sandbox;
7
8
class ResetProviders implements ResetterContract
9
{
10
    /**
11
     * @var \Illuminate\Contracts\Container\Container
12
     */
13
    protected $app;
14
15
    /**
16
     * "handle" function for resetting app.
17
     *
18
     * @param \Illuminate\Contracts\Container\Container $app
19
     * @param \SwooleTW\Http\Server\Sandbox $sandbox
20
     *
21
     * @return \Illuminate\Contracts\Container\Container
22
     */
23
    public function handle(Container $app, Sandbox $sandbox)
24
    {
25
        foreach ($sandbox->getProviders() as $provider) {
26
            $this->rebindProviderContainer($app, $provider);
27
            if (method_exists($provider, 'register')) {
28
                $provider->register();
29
            }
30
            if (method_exists($provider, 'boot')) {
31
                $app->call([$provider, 'boot']);
32
            }
33
        }
34
35
        return $app;
36
    }
37
38
    /**
39
     * Rebind service provider's container.
40
     *
41
     * @param $app
42
     * @param $provider
43
     */
44
    protected function rebindProviderContainer($app, $provider)
45
    {
46
        $closure = function () use ($app) {
47
            $this->app = $app;
48
        };
49
50
        $resetProvider = $closure->bindTo($provider, $provider);
51
        $resetProvider();
52
    }
53
}
54