Passed
Push — master ( e1e86f...c6853e )
by Curtis
05:23 queued 11s
created

Multitenant::handle()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
rs 9.5222
cc 5
nc 9
nop 2
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
use App\Service\MixedConnection;
7
use LaravelEnso\Multitenancy\App\Services\Tenant;
8
9
class Multitenant
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @param  \Closure  $next
16
     * @return mixed
17
     */
18
    public function handle($request, Closure $next)
19
    {
20
        if (! $request->user()) {
21
            return $next($request);
22
        }
23
24
        $company = $this->ownerRequestsTenant($request)
25
            ? Company::find($request->get('_tenantId'))
0 ignored issues
show
Bug introduced by
The type App\Http\Middleware\Company was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
            : $request->user()->company();
27
        if (optional($company)->isTenant()) {
28
            Tenant::set($company);
29
        }
30
31
        MixedConnection::set(
32
            $request->user(),
33
            $request->has('_tenantId')
34
        );
35
36
        if ($request->has('_tenantId')) {
37
            $request->request->remove('_tenantId');
38
        }
39
40
        return $next($request);
41
    }
42
    private function ownerRequestsTenant($request)
43
    {
44
        return $request->user()->belongsToAdminGroup()
45
            && $request->has('_tenantId');
46
    }
47
}
48