Completed
Branch master (9dff9d)
by Albert
05:30
created

ResetApplication::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Contracts\Container\Container;
6
use SwooleTW\Http\Exceptions\SandboxException;
7
use SwooleTW\Http\Server\Resetters\ResetterContract;
8
9
trait ResetApplication
10
{
11
    /**
12
     * @var \Illuminate\Config\Repository
13
     */
14
    protected $config;
15
16
    /**
17
     * @var array
18
     */
19
    protected $providers = [];
20
21
    /**
22
     * @var array
23
     */
24
    protected $resetters = [];
25
26
    /**
27
     * Set initial config.
28
     */
29
    protected function setInitialConfig()
30
    {
31
        $this->config = clone $this->getBaseApp()->make('config');
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

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