Passed
Push — master ( 5d1dbf...ac3204 )
by Paul
04:29
created

TinreServiceProvider::makeTokenGuard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Devpri\Tinre;
4
5
use Devpri\Tinre\Guards\TokenGuard;
6
use Illuminate\Auth\RequestGuard;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Gate;
9
use Illuminate\Support\ServiceProvider;
10
11
class TinreServiceProvider extends ServiceProvider
12
{
13
    /**
14
     * The policy mappings for the application.
15
     *
16
     * @var array
17
     */
18
    protected $policies = [
19
        'Devpri\Tinre\Models\User' => 'Devpri\Tinre\Policies\UserPolicy',
20
        'Devpri\Tinre\Models\Url' => 'Devpri\Tinre\Policies\UrlPolicy',
21
    ];
22
23
    /**
24
     * Perform post-registration booting of services.
25
     *
26
     * @return void
27
     */
28
    public function boot()
29
    {
30
        $this->registerRoutes();
31
32
        $this->registerPolicies();
33
34
        $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'tinre');
35
36
        $this->loadJsonTranslationsFrom(resource_path('lang/vendor/tinre'));
37
38
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'tinre');
39
40
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
41
42
        $this->listenEvents();
43
44
        // Publishing is only necessary when using the CLI.
45
        if ($this->app->runningInConsole()) {
46
            $this->bootForConsole();
47
        }
48
    }
49
50
    /**
51
     * Register any package services.
52
     *
53
     * @return void
54
     */
55
    public function register()
56
    {
57
        $this->mergeConfigFrom(__DIR__.'/../config/tinre.php', 'tinre');
58
59
        $this->registerTokenGuard();
60
61
        Tinre::addTranslation(
62
            resource_path('lang/vendor/tinre/'.app()->getLocale().'.json')
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            resource_path('lang/vendor/tinre/'.app()->/** @scrutinizer ignore-call */ getLocale().'.json')
Loading history...
63
        );
64
    }
65
66
    protected function registerRoutes()
67
    {
68
        Tinre::routes()
69
            ->withHomeRoute()
70
            ->withAuthenticationRoutes()
71
            ->withRegisterRoutes()
72
            ->withPasswordResetRoutes()
73
            ->withVerificationRoutes()
74
            ->withEmailChangeRoutes();
75
    }
76
77
    /**
78
     * Register the application's policies.
79
     *
80
     * @return void
81
     */
82
    public function registerPolicies()
83
    {
84
        foreach ($this->policies as $key => $value) {
85
            Gate::policy($key, $value);
86
        }
87
    }
88
89
    protected function listenEvents()
90
    {
91
        \Illuminate\Support\Facades\Event::listen(
92
            \Devpri\Tinre\Events\EmailChangeCreated::class,
93
            \Devpri\Tinre\Listeners\SendEmailChangeNotification::class
94
        );
95
96
        \Illuminate\Support\Facades\Event::listen(
97
            \Devpri\Tinre\Events\UserRegistered::class,
98
            \Devpri\Tinre\Listeners\SendEmailVerificationNotification::class
99
        );
100
    }
101
102
    /**
103
     * Get the services provided by the provider.
104
     *
105
     * @return array
106
     */
107
    public function provides()
108
    {
109
        return ['tinre'];
110
    }
111
112
    /**
113
     * Console-specific booting.
114
     *
115
     * @return void
116
     */
117
    protected function bootForConsole()
118
    {
119
        // Publishing the configuration file.
120
        $this->publishes([
121
            __DIR__.'/../config/tinre.php' => config_path('tinre.php'),
122
        ], 'tinre-config');
123
124
        $this->publishes([
125
            __DIR__.'/../stubs/TinreServiceProvider.stub' => app_path('Providers/TinreServiceProvider.php'),
126
        ], 'tinre-provider');
127
128
        // Publishing the views.
129
        $this->publishes([
130
            __DIR__.'/../resources/views/partials' => base_path('resources/views/vendor/tinre/partials'),
131
        ], 'tinre-views');
132
133
        // Publishing assets.
134
        $this->publishes([
135
            __DIR__.'/../public' => public_path('vendor/tinre'),
136
        ], 'tinre-assets');
137
138
        // Publishing the translation files.
139
        $this->publishes([
140
            __DIR__.'/../resources/lang' => resource_path('lang/vendor/tinre'),
141
        ], 'tinre-lang');
142
    }
143
144
    protected function registerTokenGuard()
145
    {        
146
        Auth::resolved(function ($auth) {
147
            $auth->extend('token', function ($app, $name, array $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
            $auth->extend('token', function ($app, /** @scrutinizer ignore-unused */ $name, array $config) {

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

Loading history...
Unused Code introduced by
The parameter $config is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
            $auth->extend('token', function ($app, $name, /** @scrutinizer ignore-unused */ array $config) {

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

Loading history...
Unused Code introduced by
The parameter $app is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

147
            $auth->extend('token', function (/** @scrutinizer ignore-unused */ $app, $name, array $config) {

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

Loading history...
148
                return tap($this->makeTokenGuard(), function ($guard) {
149
                    $this->app->refresh('request', $guard, 'setRequest');
150
                });
151
            });
152
        });
153
    }
154
    
155
    protected function makeTokenGuard()
156
    {
157
        return new RequestGuard(function ($request) {
158
            return (new TokenGuard(
159
                $request
160
            ))->user();
161
        }, $this->app['request']);
162
    }
163
}
164