SetTenant::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 17
rs 10
c 1
b 1
f 0
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Models\Tree;
6
use Closure;
7
use DB;
8
use Illuminate\Http\Request;
9
use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection;
10
use Spatie\Multitenancy\Models\Tenant;
11
12
class SetTenant
13
{
14
    use UsesLandlordConnection;
15
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param  \Illuminate\Http\Request  $request
20
     * @param  \Closure  $next
21
     * @return mixed
22
     */
23
    public function handle(Request $request, Closure $next)
24
    {
25
        // $user = auth()->user();
26
        // $company = DB::connection($this->getConnectionName())->table('user_company')->where('user_id', $user->id)->select('company_id')->first();
27
        // $tree = Tree::where('company_id', $company->company_id)->first();
28
        // $tenant = Tenant::where('tree_id', $tree->id)->first();
29
        // $tenant->makeCurrent();
30
        if (Tenant::checkCurrent()) {
31
            Tenant::forgetCurrent();
32
            $user = auth()->user();
33
            $company = $user->Company()->where('current_tenant', '=', 1)->first();
34
            $tree = Tree::where('current_tenant', '=', 1)->where('company_id', $company->id)->first();
35
            $tenant = Tenant::where('tree_id', $tree->id)->first();
36
            $tenant->makeCurrent();
37
        }
38
39
        return $next($request);
40
    }
41
}
42