TrackerServiceProvider::boot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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 20
    private function registerPublishableResources(): void
46
    {
47 20
        $path = dirname(__DIR__).'/../publishable';
48
        
49
        $publishable = [
50
          'config' => [
51 20
            "{$path}/config/tracker.php" => config_path('tracker.php'),
52
          ],
53
          'migrations' => [
54 20
            "{$path}/../database/migrations" => database_path('migrations')
55
          ]
56
        ];
57
        
58 20
        foreach ($publishable as $group => $paths) {
59 20
            $this->publishes($paths, $group);
60
        }
61 20
    }
62
    
63
    /**
64
     * Register configurations and facade(s).
65
     *
66
     * @return void
67
     */
68 20
    public function register(): void
69
    {
70 20
        $this->app->singleton(
71 20
            Tracker::class,
72
            function () {
73 18
                return new Tracker();
74 20
            }
75
        );
76
        
77 20
        $this->app->alias(Tracker::class, 'redbox-tracker-tracker');
78
    
79
        /**
80
         * Testing
81
         */
82 20
        if (config()->get('redbox-tracker') === null) {
83 20
            config()->set(
84 20
                'redbox-tracker',
85
                include __DIR__.'/../../publishable/config/tracker.php'
86
            );
87
        }
88
        
89 20
        if ($this->app->runningInConsole()) {
90 20
            $this->registerPublishableResources();
91
        }
92 20
    }
93
    
94
    /**
95
     * Tell Laravel where to look for the package it's migrations.
96
     *
97
     * @return void
98
     */
99 20
    public function boot(): void
100
    {
101 20
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
102
        
103 20
        $middlewareGroups = config('redbox-tracker.middleware.attach');
104
        
105 20
        foreach ($middlewareGroups as $group) {
106 20
            app('router')->pushMiddlewareToGroup($group, TrackingMiddleware::class);
107
        }
108
        
109 20
        Visitor::observe(VisitorObserver::class);
110 20
    }
111
}
112