Passed
Push — master ( 33ea8c...918c78 )
by Curtis
18:41 queued 12:15
created

Multitenant::handle()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 39
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 23
c 1
b 0
f 0
nc 17
nop 2
dl 0
loc 39
rs 8.9297
1
<?php
2
3
namespace App\Http\Middleware;
4
5
use App\Service\MixedConnection;
6
use Closure;
7
use LaravelEnso\Multitenancy\Services\Tenant;
8
// use App\Models\enso\companies\Company;
9
use LaravelEnso\Companies\Models\Company;
10
class Multitenant
11
{
12
    /**
13
     * Handle an incoming request.
14
     *
15
     * @param  \Illuminate\Http\Request  $request
16
     * @param  \Closure  $next
17
     * @return mixed
18
     */
19
    public function handle($request, Closure $next)
20
    {
21
        $db = \Session::get('companyId');
22
23
        if (! $request->user()) {
24
            return $next($request);
25
        }
26
27
        $cid = $db;
28
        $company = Company::find($cid);
29
30
        $tanent = false;
31
        if($company) {
32
            $tanent = true;
33
        }
34
        
35
        if (optional($company)->isTenant()) {
36
            Tenant::set($company);
37
        }
38
                
39
        MixedConnection::set(
40
            $request->user(),
41
            $tanent
42
            // $request->has('_tenantId')
43
        );
44
45
        if ($request->has('_tenantId')) {
46
            $request->request->remove('_tenantId');
47
        }
48
49
        $conn = \Session::get('conn');
50
        $value = \Session::get('db');
51
        if($conn === 'tenant') {
52
            $key = 'database.connections.tenant.database';
53
            config([$key => $value]);
54
            $config = json_encode(config('database.connections.'.$conn));
55
            error_log('*****************************************************'.$config);
56
        }
57
        return $next($request);
58
    }
59
60
    private function ownerRequestsTenant($request)
0 ignored issues
show
Unused Code introduced by
The method ownerRequestsTenant() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
61
    {
62
        return $request->user()->isSupervisor();
63
            // && $request->has('_tenantId');
64
    }
65
}
66