Passed
Pull Request — master (#19)
by Samuel
03:03
created

AddCustomProvider::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
3
namespace SMartins\PassportMultiauth\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class AddCustomProvider
9
{
10
    /**
11
     * The default provder of api guard.
12
     *
13
     * @var string
14
     */
15
    protected $defaultApiProvider;
16
17
    /**
18
     * Handle an incoming request. Set the `provider` from `api` guard using a
19
     * parameter `provider` coming from request. The provider on `apì` guard
20
     * is used by Laravel Passport to get the correct model on access token
21
     * creation.
22
     *
23
     * @param  \Illuminate\Http\Request  $request
24
     * @param  \Closure  $next
25
     * @return mixed
26
     */
27 9
    public function handle(Request $request, Closure $next)
28
    {
29 9
        $this->defaultApiProvider = config('auth.guards.api.provider');
30
31 9
        $params = $request->all();
32 9
        if (array_key_exists('provider', $params)) {
33 5
            if (! is_null($params['provider'])) {
34 5
                config(['auth.guards.api.provider' => $params['provider']]);
35
            }
36
        }
37
38 9
        return $next($request);
39
    }
40
41
    /**
42
     * Reset config provider to default after complete request. If necessary
43
     * can receive $request and $response params. To be used the attribute
44
     * $this->defaultApiProvider the middleware was registered on ServiceProvider
45
     * as a singleton.
46
     * Read more in https://laravel.com/docs/5.6/middleware#terminable-middleware.
47
     *
48
     * @return void
49
     */
50 9
    public function terminate()
51
    {
52 9
        config(['auth.guards.api.provider' => $this->defaultApiProvider]);
53 9
    }
54
}
55