Passed
Push — master ( 7833c1...68c609 )
by Johnny
02:33
created

TrackerServiceProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
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 67
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerPublishableResources() 0 12 2
A boot() 0 11 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 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
        ];
54
        
55 20
        foreach ($publishable as $group => $paths) {
56 20
            $this->publishes($paths, $group);
57
        }
58 20
    }
59
    
60
    /**
61
     * Register configurations and facade(s).
62
     *
63
     * @return void
64
     */
65 20
    public function register(): void
66
    {
67 20
        $this->app->singleton(
68 20
            Tracker::class,
69
            function () {
70 18
                return new Tracker();
71 20
            }
72
        );
73
        
74 20
        $this->app->alias(Tracker::class, 'redbox-tracker-tracker');
75
        
76 20
        if (config()->get('redbox-tracker') === null) {
77 20
            config()->set(
78 20
                'redbox-tracker',
79 20
                include __DIR__.'/../../publishable/config/tracker.php'
80
            );
81
        }
82
        
83 20
        if ($this->app->runningInConsole()) {
84 20
            $this->registerPublishableResources();
85
        }
86 20
    }
87
    
88
    /**
89
     * Tell Laravel where to look for the package it's migrations.
90
     *
91
     * @return void
92
     */
93 20
    public function boot(): void
94
    {
95 20
        $this->loadMigrationsFrom(realpath(__DIR__.'/../database/migrations'));
96
        
97 20
        $middlewareGroups = config('redbox-tracker.middleware.attach');
98
        
99 20
        foreach ($middlewareGroups as $group) {
100 20
            app('router')->pushMiddlewareToGroup($group, TrackingMiddleware::class);
101
        }
102
        
103 20
        Visitor::observe(VisitorObserver::class);
104 20
    }
105
}
106