Completed
Push — master ( 3609d1...2215ba )
by Marceau
9s
created

ImpersonateServiceProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 12
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\Services\ImpersonateManager;
8
9
/**
10
 * Class ServiceProvider
11
 *
12
 * @package Lab404\Impersonate
13
 */
14
class ImpersonateServiceProvider extends \Illuminate\Support\ServiceProvider
15
{
16
    /**
17
     * @var bool
18
     */
19
    protected $defer = false;
20
21
    /**
22
     * @var string
23
     */
24
    protected $configName = 'laravel-impersonate';
25
26
    /**
27
     * Register the service provider.
28
     *
29
     * @return void
30
     */
31
    public function register()
32
    {
33
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
34
35
        $this->mergeConfigFrom($configPath, $this->configName);
36
37
        $this->app->bind(ImpersonateManager::class, ImpersonateManager::class);
38
39
        $this->app->singleton(ImpersonateManager::class, function ($app)
40
        {
41
            return new ImpersonateManager($app);
42
        });
43
44
        $this->app->alias(ImpersonateManager::class, 'impersonate');
45
46
        $router = $this->app['router'];
47
        $router->macro('impersonate', function () use ($router) {
48
            $router->get('/impersonate/take/{id}', '\Lab404\Impersonate\Controllers\[email protected]')->name('impersonate');
49
            $router->get('/impersonate/leave', '\Lab404\Impersonate\Controllers\[email protected]')->name('impersonate.leave');
50
        });
51
52
        $this->registerBladeDirectives();
53
    }
54
55
    /**
56
     * Bootstrap the application events.
57
     *
58
     * @return void
59
     */
60
    public function boot()
61
    {
62
        $configPath = __DIR__ . '/../config/' . $this->configName . '.php';
63
64
        $this->publishes([$configPath => config_path($this->configName . '.php')], 'impersonate');
65
    }
66
67
    /**
68
     * Register plugin blade directives.
69
     *
70
     * @param   void
71
     * @return  void`
0 ignored issues
show
Documentation introduced by MarceauKa
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...
72
     */
73
    protected function registerBladeDirectives()
74
    {
75
        Blade::directive('impersonating', function($expression) {
0 ignored issues
show
Unused Code introduced by MarceauKa
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...
76
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->isImpersonated()): ?>';
77
        });
78
79
        Blade::directive('endImpersonating', function($expression) {
0 ignored issues
show
Unused Code introduced by MarceauKa
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...
80
            return '<?php endif; ?>';
81
        });
82
83
        Blade::directive('canImpersonate', function($expression) {
0 ignored issues
show
Unused Code introduced by MarceauKa
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...
84
            return '<?php if (app()["auth"]->check() && app()["auth"]->user()->canImpersonate()): ?>';
85
        });
86
87
        Blade::directive('endCanImpersonate', function($expression) {
0 ignored issues
show
Unused Code introduced by MarceauKa
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...
88
            return '<?php endif; ?>';
89
        });
90
    }
91
}