ResetApplication::setInitialProviders()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Container\Container;
7
use SwooleTW\Http\Exceptions\SandboxException;
8
use SwooleTW\Http\Server\Resetters\ResetterContract;
9
10
trait ResetApplication
11
{
12
    /**
13
     * @var \Illuminate\Config\Repository
14
     */
15
    protected $config;
16
17
    /**
18
     * @var array
19
     */
20
    protected $providers = [];
21
22
    /**
23
     * @var \SwooleTW\Http\Server\Resetters\ResetterContract[]|array
24
     */
25
    protected $resetters = [];
26
27
    /**
28
     * Set initial config.
29
     */
30
    protected function setInitialConfig()
31
    {
32
        $this->config = clone $this->getBaseApp()->make(Repository::class);
0 ignored issues
show
Bug introduced by
It seems like getBaseApp() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        $this->config = clone $this->/** @scrutinizer ignore-call */ getBaseApp()->make(Repository::class);
Loading history...
33
    }
34
35
    /**
36
     * Get config snapshot.
37
     */
38
    public function getConfig()
39
    {
40
        return $this->config;
41
    }
42
43
    /**
44
     * Initialize customized service providers.
45
     */
46
    protected function setInitialProviders()
47
    {
48
        $app = $this->getBaseApp();
49
        $providers = $this->config->get('swoole_http.providers', []);
50
51
        foreach ($providers as $provider) {
52
            if (class_exists($provider) && ! in_array($provider, $this->providers)) {
53
                $providerClass = new $provider($app);
54
                $this->providers[$provider] = $providerClass;
55
            }
56
        }
57
    }
58
59
    /**
60
     * Get Initialized providers.
61
     */
62
    public function getProviders()
63
    {
64
        return $this->providers;
65
    }
66
67
    /**
68
     * Initialize resetters.
69
     */
70
    protected function setInitialResetters()
71
    {
72
        $app = $this->getBaseApp();
73
        $resetters = $this->config->get('swoole_http.resetters', []);
74
75
        foreach ($resetters as $resetter) {
76
            $resetterClass = $app->make($resetter);
77
            if (! $resetterClass instanceof ResetterContract) {
78
                throw new SandboxException("{$resetter} must implement " . ResetterContract::class);
79
            }
80
            $this->resetters[$resetter] = $resetterClass;
81
        }
82
    }
83
84
    /**
85
     * Get Initialized resetters.
86
     */
87
    public function getResetters()
88
    {
89
        return $this->resetters;
90
    }
91
92
    /**
93
     * Reset Laravel/Lumen Application.
94
     *
95
     * @param \Illuminate\Contracts\Container\Container $app
96
     */
97
    public function resetApp(Container $app)
98
    {
99
        foreach ($this->resetters as $resetter) {
100
            $resetter->handle($app, $this);
101
        }
102
    }
103
}
104