JWTAuthServiceProvider::bootBindings()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 42
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
namespace Tymon\JWTAuth\Providers;
4
5
use Tymon\JWTAuth\JWTAuth;
6
use Tymon\JWTAuth\Blacklist;
7
use Tymon\JWTAuth\JWTManager;
8
use Tymon\JWTAuth\PayloadFactory;
9
use Tymon\JWTAuth\Claims\Factory;
10
use Illuminate\Support\ServiceProvider;
11
use Tymon\JWTAuth\Commands\JWTGenerateCommand;
12
use Tymon\JWTAuth\Validators\PayloadValidator;
13
14
class JWTAuthServiceProvider extends ServiceProvider
15
{
16
    /**
17
     * Indicates if loading of the provider is deferred.
18
     *
19
     * @var bool
20
     */
21
    protected $defer = false;
22
23
    /**
24
     * Boot the service provider.
25
     */
26
    public function boot()
27
    {
28
        $this->publishes([
29
            __DIR__.'/../config/config.php' => config_path('jwt.php')
30
        ], 'config');
31
32
        $this->bootBindings();
33
34
        $this->commands('tymon.jwt.generate');
35
    }
36
37
    /**
38
     * Bind some Interfaces and implementations
39
     */
40
    protected function bootBindings()
41
    {
42
        $this->app['Tymon\JWTAuth\JWTAuth'] = function ($app) {
43
            return $app['tymon.jwt.auth'];
44
        };
45
46
        $this->app['Tymon\JWTAuth\Providers\User\UserInterface'] = function ($app) {
47
            return $app['tymon.jwt.provider.user'];
48
        };
49
50
        $this->app['Tymon\JWTAuth\Providers\JWT\JWTInterface'] = function ($app) {
51
            return $app['tymon.jwt.provider.jwt'];
52
        };
53
54
        $this->app['Tymon\JWTAuth\Providers\Auth\AuthInterface'] = function ($app) {
55
            return $app['tymon.jwt.provider.auth'];
56
        };
57
58
        $this->app['Tymon\JWTAuth\Providers\Storage\StorageInterface'] = function ($app) {
59
            return $app['tymon.jwt.provider.storage'];
60
        };
61
62
        $this->app['Tymon\JWTAuth\JWTManager'] = function ($app) {
63
            return $app['tymon.jwt.manager'];
64
        };
65
66
        $this->app['Tymon\JWTAuth\Blacklist'] = function ($app) {
67
            return $app['tymon.jwt.blacklist'];
68
        };
69
70
        $this->app['Tymon\JWTAuth\PayloadFactory'] = function ($app) {
71
            return $app['tymon.jwt.payload.factory'];
72
        };
73
74
        $this->app['Tymon\JWTAuth\Claims\Factory'] = function ($app) {
75
            return $app['tymon.jwt.claim.factory'];
76
        };
77
78
        $this->app['Tymon\JWTAuth\Validators\PayloadValidator'] = function ($app) {
79
            return $app['tymon.jwt.validators.payload'];
80
        };
81
    }
82
83
    /**
84
     * Register the service provider.
85
     *
86
     * @return void
87
     */
88
    public function register()
89
    {
90
        // register providers
91
        $this->registerUserProvider();
92
        $this->registerJWTProvider();
93
        $this->registerAuthProvider();
94
        $this->registerStorageProvider();
95
        $this->registerJWTBlacklist();
96
97
        $this->registerClaimFactory();
98
        $this->registerJWTManager();
99
100
        $this->registerJWTAuth();
101
        $this->registerPayloadValidator();
102
        $this->registerPayloadFactory();
103
        $this->registerJWTCommand();
104
105
        $this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'jwt');
106
    }
107
108
    /**
109
     * Register the bindings for the User provider
110
     */
111
    protected function registerUserProvider()
112
    {
113
        $this->app['tymon.jwt.provider.user'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
            return $app->make($this->config('providers.user'), [$app->make($this->config('user'))]);
115
        });
116
    }
117
118
    /**
119
     * Register the bindings for the JSON Web Token provider
120
     */
121
    protected function registerJWTProvider()
122
    {
123
        $this->app['tymon.jwt.provider.jwt'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
125
            $secret = $this->config('secret');
126
            $algo = $this->config('algo');
127
            $provider = $this->config('providers.jwt');
128
129
            return $app->make($provider, [$secret, $algo]);
130
        });
131
    }
132
133
    /**
134
     * Register the bindings for the Auth provider
135
     */
136
    protected function registerAuthProvider()
137
    {
138
        $this->app['tymon.jwt.provider.auth'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The parameter $app 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...
139
            return $this->getConfigInstance($this->config('providers.auth'));
140
        });
141
    }
142
143
    /**
144
     * Register the bindings for the Storage provider
145
     */
146
    protected function registerStorageProvider()
147
    {
148
        $this->app['tymon.jwt.provider.storage'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Unused Code introduced by
The parameter $app 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...
149
            return $this->getConfigInstance($this->config('providers.storage'));
150
        });
151
    }
152
153
    /**
154
     * Register the bindings for the Payload Factory
155
     */
156
    protected function registerClaimFactory()
157
    {
158
        $this->app->singleton('tymon.jwt.claim.factory', function () {
159
            return new Factory();
160
        });
161
    }
162
163
    /**
164
     * Register the bindings for the JWT Manager
165
     */
166
    protected function registerJWTManager()
167
    {
168
        $this->app['tymon.jwt.manager'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169
170
            $instance = new JWTManager(
171
                $app['tymon.jwt.provider.jwt'],
172
                $app['tymon.jwt.blacklist'],
173
                $app['tymon.jwt.payload.factory']
174
            );
175
176
            return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled'));
177
        });
178
    }
179
180
    /**
181
     * Register the bindings for the main JWTAuth class
182
     */
183
    protected function registerJWTAuth()
184
    {
185
        $this->app['tymon.jwt.auth'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
187
            $auth = new JWTAuth(
188
                $app['tymon.jwt.manager'],
189
                $app['tymon.jwt.provider.user'],
190
                $app['tymon.jwt.provider.auth'],
191
                $app['request']
192
            );
193
194
            return $auth->setIdentifier($this->config('identifier'));
195
        });
196
    }
197
198
    /**
199
     * Register the bindings for the main JWTAuth class
200
     */
201
    protected function registerJWTBlacklist()
202
    {
203
        $this->app['tymon.jwt.blacklist'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
204
            return new Blacklist($app['tymon.jwt.provider.storage']);
205
        });
206
    }
207
208
    /**
209
     * Register the bindings for the payload validator
210
     */
211
    protected function registerPayloadValidator()
212
    {
213
        $this->app['tymon.jwt.validators.payload'] = $this->app->share(function () {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
214
            return with(new PayloadValidator())->setRefreshTTL($this->config('refresh_ttl'))->setRequiredClaims($this->config('required_claims'));
215
        });
216
    }
217
218
    /**
219
     * Register the bindings for the Payload Factory
220
     */
221
    protected function registerPayloadFactory()
222
    {
223
        $this->app['tymon.jwt.payload.factory'] = $this->app->share(function ($app) {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
224
            $factory = new PayloadFactory($app['tymon.jwt.claim.factory'], $app['request'], $app['tymon.jwt.validators.payload']);
225
226
            return $factory->setTTL($this->config('ttl'));
227
        });
228
    }
229
230
    /**
231
     * Register the Artisan command
232
     */
233
    protected function registerJWTCommand()
234
    {
235
        $this->app['tymon.jwt.generate'] = $this->app->share(function () {
0 ignored issues
show
Bug introduced by
The method share() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
            return new JWTGenerateCommand();
237
        });
238
    }
239
240
    /**
241
     * Helper to get the config values
242
     *
243
     * @param  string $key
244
     * @return string
245
     */
246
    protected function config($key, $default = null)
247
    {
248
        return config("jwt.$key", $default);
249
    }
250
251
    /**
252
     * Get an instantiable configuration instance. Pinched from dingo/api :)
253
     *
254
     * @param  mixed  $instance
255
     * @return object
256
     */
257
    protected function getConfigInstance($instance)
258
    {
259
        if (is_callable($instance)) {
260
            return call_user_func($instance, $this->app);
261
        } elseif (is_string($instance)) {
262
            return $this->app->make($instance);
263
        }
264
265
        return $instance;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $instance; (object|integer|double|null|array|boolean) is incompatible with the return type documented by Tymon\JWTAuth\Providers\...ider::getConfigInstance of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
266
    }
267
}
268