Passed
Push — master ( a29117...ebc4ee )
by Johnny
02:26
created

TrackerServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 23
c 1
b 0
f 0
dl 0
loc 68
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 2
A registerPublishableResources() 0 12 2
A register() 0 20 3
1
<?php
2
3
/**
4
 * TrackerServiceProvider.php
5
 *
6
 * The main purpose of this service provider is to make sure laravel
7
 * knows where our publishable resources are in our package.
8
 *
9
 * PHP version 7.2
10
 *
11
 * @category Providers
12
 * @package  RedboxTracker
13
 * @author   Johnny Mast <[email protected]>
14
 * @license  https://opensource.org/licenses/MIT MIT
15
 * @link     https://github.com/johnnymast/redbox-tracker
16
 * @since    GIT:1.0
17
 */
18
19
namespace Redbox\Tracker\Providers;
20
21
use Illuminate\Support\ServiceProvider;
22
use Redbox\Tracker\Middleware\TrackingMiddleware;
23
use Redbox\Tracker\Observers\VisitorObserver as VisitorObserver;
24
use Redbox\Tracker\Tracker;
25
use Redbox\Tracker\Visitor;
26
27
/**
28
 * Class TrackerServiceProvider
29
 *
30
 * @category Providers
31
 * @package  RedboxTracker
32
 * @author   Johnny Mast <[email protected]>
33
 * @license  https://opensource.org/licenses/MIT MIT
34
 * @link     https://github.com/johnnymast/redbox-tracker
35
 * @since    GIT:1.0
36
 */
37
class TrackerServiceProvider extends ServiceProvider
38
{
39
40
    /**
41
     * Register the publishable files.
42
     *
43
     * @return void
44
     */
45 16
    private function registerPublishableResources(): void
46
    {
47 16
        $path = dirname(__DIR__) . '/../../publishable';
48
49
        $publishable = [
50
          'config' => [
51 16
            "{$path}/config/tracker.php" => config_path('tracker.php'),
52
          ],
53
        ];
54
55 16
        foreach ($publishable as $group => $paths) {
56 16
            $this->publishes($paths, $group);
57
        }
58 16
    }
59
60
61
    /**
62
     * Register configurations and facade(s).
63
     *
64
     * @return void
65
     */
66 16
    public function register(): void
67
    {
68 16
        $this->app->singleton(
69 16
            Tracker::class,
70
            function () {
71 14
                return new Tracker();
72 16
            }
73
        );
74
75 16
        $this->app->alias(Tracker::class, 'redbox-tracker-tracker');
76
77 16
        if ($this->app->config->get('redbox-tracker') === null) {
0 ignored issues
show
Bug introduced by
Accessing config on the interface Illuminate\Contracts\Foundation\Application suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
78 16
            $this->app->config->set(
79 16
                'redbox-tracker',
80 16
                include __DIR__ . '/../../publishable/config/tracker.php'
81
            );
82
        }
83
84 16
        if ($this->app->runningInConsole()) {
85 16
            $this->registerPublishableResources();
86
        }
87 16
    }
88
89
    /**
90
     * Tell Laravel where to look for the package it's migrations.
91
     *
92
     * @return void
93
     */
94 16
    public function boot(): void
95
    {
96 16
        $this->loadMigrationsFrom(realpath(__DIR__.'/../database/migrations'));
97
    
98 16
        $middlewareGroups = config('redbox-tracker.middleware.attach');
99
    
100 16
        foreach ($middlewareGroups as $group) {
101 16
            app('router')->pushMiddlewareToGroup($group, TrackingMiddleware::class);
102
        }
103
        
104 16
        Visitor::observe(VisitorObserver::class);
105 16
    }
106
}
107