MultiTenantMiddleware::handle()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace MultiTenantLaravel\App\Http\Middleware;
4
5
use Closure;
6
7
class MultiTenantMiddleware
8
{
9
    /**
10
     * Handle multi tenant middleware
11
     *
12
     * @param $request
13
     * @param Closure $next
14
     * @return void
15
     */
16
    public function handle($request, Closure $next)
17
    {
18
        // Here we can perform an multi tenant required authentication
19
        if (!\Auth::check()) {
20
            return redirect('/login');
21
        }
22
23
        // Assign the user to a tenant if we have one
24
        if (\Auth::user()->owns()->count() == 1) {
25
            session()->put('tenant', [
26
                'id' => \Auth::user()->owns()->first()->id
27
            ]);
28
        }
29
30
        return $next($request);
31
    }
32
}
33