Tenantable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 23 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Tenants\Http\Middleware;
6
7
use Closure;
8
9
class Tenantable
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param \Illuminate\Http\Request $request
15
     * @param \Closure                 $next
16
     *
17
     * @return mixed
18
     */
19
    public function handle($request, Closure $next)
20
    {
21
        $subdomain = $request->route('subdomain');
22
        $tenant = app('rinvex.tenants.tenant')->where('slug', $subdomain)->first();
23
24
        if ($subdomain && ! $tenant) {
25
            return intend([
26
                'url' => route('frontarea.home'),
27
                'with' => ['warning' => trans('cortex/foundation::messages.resource_not_found', ['resource' => trans('cortex/tenants::common.tenant'), 'identifier' => $subdomain])],
28
            ]);
29
        }
30
31
        // Scope bouncer
32
        (! $tenant || ! app()->bound(\Silber\Bouncer\Bouncer::class)) || app(\Silber\Bouncer\Bouncer::class)->scope()->to($tenant->getKey());
33
34
        // unBind {subdomain} route parameter
35
        ! $request->route('subdomain') || $request->route()->forgetParameter('subdomain');
36
37
        // Activate current tenant
38
        ! $tenant || config(['rinvex.tenants.active' => $tenant]);
39
40
        return $next($request);
41
    }
42
}
43