Completed
Push — master ( 2215ba...544b6d )
by
unknown
11s
created

ImpersonateServiceProvider::register()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 13
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\CantAccesIfImpersonate;
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
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
35
36
        $this->mergeConfigFrom($configPath, $this->configName);
37
38
        $this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
39
40
        $this->app->singleton(ImpersonateManager::class, function ($app)
41
        {
42
            return new ImpersonateManager($app);
43
        });
44
45
        $this->app->alias(ImpersonateManager::class, 'impersonate');
46
47
        $router = $this->app['router'];
48
        $router->macro('impersonate', function () use ($router) {
49
            $router->get('/impersonate/take/{id}', '\Lab404\Impersonate\Controllers\ImpersonateController@take')->name('impersonate');
50
            $router->get('/impersonate/leave', '\Lab404\Impersonate\Controllers\ImpersonateController@leave')->name('impersonate.leave');
51
        });
52
53
        $this->registerBladeDirectives();
54
55
        $this->app['router']->aliasMiddleware('impersonate.protect', CantAccesIfImpersonate::class);
56
    }
57
58
    /**
59
     * Bootstrap the application events.
60
     *
61
     * @return void
62
     */
63
    public function boot()
64
    {
65
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
66
67
        $this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
68
    }
69
70
    /**
71
     * Register plugin blade directives.
72
     *
73
     * @param   void
74
     * @return  void`
0 ignored issues
show
Documentation introduced by
The doc-type void` could not be parsed: Unknown type name "void`" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
75
     */
76
    protected function registerBladeDirectives()
77
    {
78
        Blade::directive('impersonating', function($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
79
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->isImpersonated()): ?>';
80
        });
81
82
        Blade::directive('endImpersonating', function($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
            return '<?php endif; ?>';
84
        });
85
86
        Blade::directive('canImpersonate', function($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->canImpersonate()): ?>';
88
        });
89
90
        Blade::directive('endCanImpersonate', function($expression) {
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
91
            return '<?php endif; ?>';
92
        });
93
    }
94
}