Completed
Push — master ( aa2929...68dae7 )
by Marceau
03:14
created

ImpersonateServiceProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Lab404\Impersonate;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\Support\Facades\Blade;
7
use Lab404\Impersonate\Middleware\ProtectFromImpersonation;
8
use Lab404\Impersonate\Services\ImpersonateManager;
9
10
/**
11
 * Class ServiceProvider
12
 *
13
 * @package Lab404\Impersonate
14
 */
15
class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
16
{
17
    /**
18
     * @var bool
19
     */
20
    protected $defer = false;
21
22
    /**
23
     * @var string
24
     */
25
    protected $configName = 'laravel-impersonate';
26
27
    /**
28
     * Register the service provider.
29
     *
30
     * @return void
31
     */
32
    public function register()
33
    {
34
        $this->mergeConfig();
35
36
        $this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
37
38
        $this->app->singleton(ImpersonateManager::class, function ($app)
39
        {
40
            return new ImpersonateManager($app);
41
        });
42
43
        $this->app->alias(ImpersonateManager::class, 'impersonate');
44
45
        $this->registerRoutesMacro();
46
        $this->registerBladeDirectives();
47
        $this->registerMiddleware();
48
    }
49
50
    /**
51
     * Bootstrap the application events.
52
     *
53
     * @return void
54
     */
55
    public function boot()
56
    {
57
        $this->publishConfig();
58
    }
59
60
    /**
61
     * Register plugin blade directives.
62
     *
63
     * @param   void
64
     * @return  void
65
     */
66
    protected function registerBladeDirectives()
67
    {
68
        Blade::directive('impersonating', function() {
69
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->isImpersonated()): ?>';
70
        });
71
72
        Blade::directive('endImpersonating', function() {
73
            return '<?php endif; ?>';
74
        });
75
76
        Blade::directive('canImpersonate', function() {
77
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->canImpersonate()): ?>';
78
        });
79
80
        Blade::directive('endCanImpersonate', function() {
81
            return '<?php endif; ?>';
82
        });
83
    }
84
85
    /**
86
     * Register routes macro.
87
     *
88
     * @param   void
89
     * @return  void
90
     */
91
    protected function registerRoutesMacro()
92
    {
93
        $router = $this->app['router'];
94
95
        $router->macro('impersonate', function () use ($router) {
96
            $router->get('/impersonate/take/{id}', '\Lab404\Impersonate\Controllers\ImpersonateController@take')->name('impersonate');
97
            $router->get('/impersonate/leave', '\Lab404\Impersonate\Controllers\ImpersonateController@leave')->name('impersonate.leave');
98
        });
99
    }
100
101
    /**
102
     * Register plugin middleware.
103
     *
104
     * @param   void
105
     * @return  void
106
     */
107
    public function registerMiddleware()
108
    {
109
        $this->app['router']->aliasMiddleware('impersonate.protect', ProtectFromImpersonation::class);
110
    }
111
112
    /**
113
     * Merge config file.
114
     *
115
     * @param   void
116
     * @return  void
117
     */
118
    protected function mergeConfig()
119
    {
120
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
121
122
        $this->mergeConfigFrom($configPath, $this->configName);
123
    }
124
125
    /**
126
     * Publish config file.
127
     *
128
     * @param   void
129
     * @return  void
130
     */
131
    protected function publishConfig()
132
    {
133
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
134
135
        $this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
136
    }
137
}