JwtGuardServiceProvider   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 2
cbo 3
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 13 1
A publishConfig() 0 10 1
A publishMigration() 0 6 1
A register() 0 10 1
1
<?php
2
3
namespace WWON\JwtGuard\Providers;
4
5
use Illuminate\Support\Facades\Auth;
6
use Illuminate\Support\ServiceProvider;
7
use WWON\JwtGuard\Contract\ClaimManager as ClaimManagerContract;
8
use WWON\JwtGuard\JwtGuard;
9
use WWON\JwtGuard\JwtService;
10
use WWON\JwtGuard\ClaimManager;
11
12
class JwtGuardServiceProvider extends ServiceProvider
13
{
14
15
    /**
16
     * Indicates if loading of the provider is deferred.
17
     *
18
     * @var bool
19
     */
20
    protected $defer = false;
21
22
    /**
23
     * Boot the service provider.
24
     */
25
    public function boot()
26
    {
27
        Auth::extend('jwt', function($app, $name, array $config) {
28
            return new JwtGuard(
29
                $app['auth']->createUserProvider($config['provider']),
30
                $app[JwtService::class],
31
                $app['request']
32
            );
33
        });
34
35
        $this->publishConfig();
36
        $this->publishMigration();
37
    }
38
39
    /**
40
     * Publish the configuration file.
41
     */
42
    private function publishConfig()
43
    {
44
        $configFile = __DIR__ . '/../config/jwt.php';
45
46
        $this->publishes([
47
            $configFile => config_path('jwt.php')
48
        ], 'config');
49
50
        $this->mergeConfigFrom($configFile, 'jwt');
51
    }
52
53
    /**
54
     * Publish the migration file.
55
     */
56
    private function publishMigration()
57
    {
58
        $this->publishes([
59
            __DIR__ . '/../database/migrations/' => database_path('migrations')
60
        ], 'migrations');
61
    }
62
63
    /**
64
     * Register any application services.
65
     */
66
    public function register()
67
    {
68
        $this->app->bind(ClaimManagerContract::class, ClaimManager::class);
69
70
        $this->app->rebinding('request', function ($app, $request) {
71
            $request->setUserResolver(function ($guard = null) {
72
                return auth()->guard($guard)->user();
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
            });
74
        });
75
    }
76
77
}