Test Failed
Pull Request — master (#154)
by Antonio Carlos
04:56 queued 02:28
created

ServiceProvider   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 355
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 355
ccs 0
cts 113
cp 0
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 9

20 Methods

Rating   Name   Duplication   Size   Complexity  
A configurePaths() 0 32 1
A configureViews() 0 7 1
A createResourceChecker() 0 11 1
A getCacheClosure() 0 8 1
A provides() 0 9 1
A createHealthService() 0 15 1
A getResourceCheckerClosure() 0 8 1
A getRoutes() 0 4 1
A instantiateCommands() 0 6 1
A instantiateService() 0 4 1
A mergeConfig() 0 10 2
A register() 0 18 1
A registerResourcesRoutes() 0 10 2
A registerConsoleCommands() 0 7 1
A registerEventListeners() 0 4 1
A registerRoutes() 0 8 1
A registerServices() 0 18 1
A createServices() 0 6 1
A registerTasks() 0 14 4
A addDistPathToConfig() 0 4 1
1
<?php
2
3
namespace PragmaRX\Health;
4
5
use Event;
6
use PragmaRX\Yaml\Package\Yaml;
7
use PragmaRX\Health\Support\Cache;
8
use Illuminate\Console\Scheduling\Schedule;
9
use PragmaRX\Health\Support\ResourceLoader;
10
use PragmaRX\Health\Support\Traits\Routing;
11
use PragmaRX\Health\Events\RaiseHealthIssue;
12
use PragmaRX\Health\Support\ResourceChecker;
13
use PragmaRX\Health\Listeners\NotifyHealthIssue;
14
use PragmaRX\Health\Console\Commands as ConsoleCommands;
15
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
16
17
class ServiceProvider extends IlluminateServiceProvider
18
{
19
    use Routing;
20
21
    /**
22
     * The application instance.
23
     *
24
     * @var \Illuminate\Foundation\Application
25
     */
26
    protected $app;
27
28
    /**
29
     * Resource loader instance.
30
     *
31
     * @var
32
     */
33
    protected $resourceLoader;
34
35
    /**
36
     * The health service.
37
     *
38
     * @var
39
     */
40
    private $healthService;
41
42
    /**
43
     * All artisan commands.
44
     *
45
     * @var
46
     */
47
    private $commands;
48
49
    /**
50
     * The router.
51
     *
52
     * @var
53
     */
54
    private $router;
55
56
    /**
57
     * Cache closure.
58
     *
59
     * @var
60
     */
61
    private $cacheClosure;
62
63
    /**
64
     * Resource checker closure.
65
     *
66
     * @var
67
     */
68
    private $resourceCheckerClosure;
69
70
    /**
71
     * Health service closure.
72
     *
73
     * @var
74
     */
75
    private $healthServiceClosure;
76
77
    /**
78
     * Configure package paths.
79
     */
80
    private function configurePaths()
81
    {
82
        $this->publishes(
83
            [
84
                __DIR__.'/config/health.php' => config_path(
85
                    'health/config.php'
86
                ),
87
                __DIR__.'/config/resources/' => config_path(
88
                    'health/resources/'
89
                ),
90
            ],
91
            'config'
92
        );
93
94
        $this->publishes(
95
            [
96
                __DIR__.'/resources/views/' => resource_path(
97
                    'views/vendor/pragmarx/health/'
98
                ),
99
            ],
100
            'views'
101
        );
102
103
        $this->publishes(
104
            [
105
                __DIR__.'/database/migrations/' => database_path(
106
                    'migrations'
107
                ),
108
            ],
109
            'migrations'
110
        );
111
    }
112
113
    /**
114
     * Configure package folder views.
115
     */
116
    private function configureViews()
117
    {
118
        $this->loadViewsFrom(
119
            realpath(__DIR__.'/resources/views'),
120
            'pragmarx/health'
121
        );
122
    }
123
124
    /**
125
     * Create health service.
126
     */
127
    private function createHealthService()
128
    {
129
        $resourceChecker = call_user_func($this->resourceCheckerClosure);
130
131
        $cache = call_user_func($this->cacheClosure);
132
133
        $this->healthServiceClosure = function () use (
134
            $resourceChecker,
135
            $cache
136
        ) {
137
            return $this->instantiateService($resourceChecker, $cache);
138
        };
139
140
        $this->healthService = call_user_func($this->healthServiceClosure);
141
    }
142
143
    /**
144
     * Create resource checker.
145
     */
146
    private function createResourceChecker()
147
    {
148
        $this->resourceLoader = new ResourceLoader(new Yaml());
149
150
        $this->cacheClosure = $this->getCacheClosure();
151
152
        $this->resourceCheckerClosure = $this->getResourceCheckerClosure(
153
            $this->resourceLoader,
154
            call_user_func($this->cacheClosure)
155
        );
156
    }
157
158
    /**
159
     * Get the cache closure for instantiation.
160
     *
161
     * @return \Closure
162
     */
163
    private function getCacheClosure()
164
    {
165
        $cacheClosure = function () {
166
            return new Cache();
167
        };
168
169
        return $cacheClosure;
170
    }
171
172
    /**
173
     * Get the resource checker closure for instantiation.
174
     *
175
     * @param $resourceLoader
176
     * @param $cache
177
     * @return \Closure
178
     */
179
    private function getResourceCheckerClosure($resourceLoader, $cache)
180
    {
181
        $resourceCheckerInstance = function () use ($resourceLoader, $cache) {
182
            return new ResourceChecker($resourceLoader, $cache);
183
        };
184
185
        return $resourceCheckerInstance;
186
    }
187
188
    /**
189
     * Get the list of routes.
190
     *
191
     * @return array
192
     */
193
    private function getRoutes()
194
    {
195
        return config('health.routes.list');
196
    }
197
198
    /**
199
     * Instantiate commands.
200
     *
201
     * @return \Illuminate\Foundation\Application|mixed
202
     */
203
    private function instantiateCommands()
204
    {
205
        return $this->commands = instantiate(Commands::class, [
206
            $this->healthService,
207
        ]);
208
    }
209
210
    /**
211
     * Instantiate the main service.
212
     *
213
     * @param $resourceChecker
214
     * @param $cache
215
     * @return Service
216
     */
217
    private function instantiateService($resourceChecker, $cache)
218
    {
219
        return $this->healthService = new Service($resourceChecker, $cache);
220
    }
221
222
    /**
223
     * Merge configuration.
224
     */
225
    private function mergeConfig()
226
    {
227
        if (file_exists(config_path('/health/config.php'))) {
228
            $this->mergeConfigFrom(config_path('/health/config.php'), 'health');
229
        }
230
231
        $this->mergeConfigFrom(__DIR__.'/config/health.php', 'health');
232
233
        $this->addDistPathToConfig();
234
    }
235
236
    /**
237
     * Register any application services.
238
     *
239
     * @return void
240
     */
241
    public function register()
242
    {
243
        $this->mergeConfig();
244
245
        $this->configureViews();
246
247
        $this->configurePaths();
248
249
        $this->registerServices();
250
251
        $this->registerRoutes();
252
253
        $this->registerTasks();
254
255
        $this->registerEventListeners();
256
257
        $this->registerConsoleCommands();
258
    }
259
260
    private function registerResourcesRoutes()
261
    {
262
        collect($this->resourceLoader->getResources())->each(function ($item) {
263
            if (isset($item['routes'])) {
264
                collect($item['routes'])->each(function ($route, $key) {
265
                    $this->registerRoute($route, $key);
266
                });
267
            }
268
        });
269
    }
270
271
    /**
272
     * Register console commands.
273
     */
274
    private function registerConsoleCommands()
275
    {
276
        $this->commands([
277
            ConsoleCommands\HealthPanelCommand::class,
278
            ConsoleCommands\HealthCheckCommand::class,
279
        ]);
280
    }
281
282
    /**
283
     * Register event listeners.
284
     */
285
    private function registerEventListeners()
286
    {
287
        Event::listen(RaiseHealthIssue::class, NotifyHealthIssue::class);
288
    }
289
290
    /**
291
     * Register routes.
292
     */
293
    private function registerRoutes()
294
    {
295
        collect(($routes = $this->getRoutes()))->each(function ($route) {
296
            $this->registerRoute($route);
297
        });
298
299
        $this->registerResourcesRoutes();
300
    }
301
302
    /**
303
     * Register service.
304
     */
305
    private function registerServices()
306
    {
307
        $this->createServices();
308
309
        $this->app->singleton('pragmarx.health.cache', $this->cacheClosure);
310
311
        $this->app->singleton(
312
            'pragmarx.health.resource.checker',
313
            $this->resourceCheckerClosure
314
        );
315
316
        $this->app->singleton('pragmarx.health', $this->healthServiceClosure);
317
318
        $this->app->singleton(
319
            'pragmarx.health.commands',
320
            $this->instantiateCommands()
321
        );
322
    }
323
324
    /**
325
     * Create services.
326
     */
327
    public function createServices()
328
    {
329
        $this->createResourceChecker();
330
331
        $this->createHealthService();
332
    }
333
334
    /**
335
     * Register scheduled tasks.
336
     */
337
    private function registerTasks()
338
    {
339
        if (
340
            config('health.scheduler.enabled') &&
341
            ($frequency = config('health.scheduler.frequency')) &&
342
            config('health.notifications.enabled')
343
        ) {
344
            $scheduler = instantiate(Schedule::class);
345
346
            $scheduler
347
                ->call($this->healthService->getSilentChecker())
348
                ->$frequency();
349
        }
350
    }
351
352
    /**
353
     * Get the services provided by the provider.
354
     *
355
     * @return array
356
     */
357
    public function provides()
358
    {
359
        return [
360
            'pragmarx.health.cache',
361
            'pragmarx.health.resource.checker',
362
            'pragmarx.health',
363
            'pragmarx.health.commands',
364
        ];
365
    }
366
367
    public function addDistPathToConfig()
368
    {
369
        config(['health.dist_path' => __DIR__.'/resources/dist']);
370
    }
371
}
372