Issues (364)

app/Http/Middleware/Multitenant.php (4 issues)

1
<?php
2
3
namespace App\Http\Middleware;
4
5
use Closure;
6
7
// use App\Models\enso\companies\Company;
8
9
class Multitenant
10
{
11
    /**
12
     * Handle an incoming request.
13
     *
14
     * @param  \Illuminate\Http\Request  $request
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next)
18
    {
19
//        $tenatDB=\App\Models\Tenant::where('id',Auth::id())->get();
20
//        Log::debug();
21
22
        $user = \Auth::user();
23
\Session::get('conn');
24
        //$value = \Session::get('db');
25
        if ($user->isAdmin()) {
0 ignored issues
show
The method isAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
        if ($user->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
26
            $key = 'database.connections.mysql.database';
27
            $value = env('DB_DATABASE', 'enso');
28
        } else {
29
            $key = 'database.connections.tenantdb.database';
30
            if (session()->get('db')) {
31
                $value = session()->get('db');
32
            } else {
33
                $company = $user->person->company();
0 ignored issues
show
Accessing person on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
34
                // dd($company);
35
                $tenants = \App\Models\Tenant::find($company->id);
36
                // dd($tenants);
37
                $value = $tenants->tenancy_db_name;
0 ignored issues
show
The property tenancy_db_name does not seem to exist on Illuminate\Database\Eloq...gHasThroughRelationship.
Loading history...
Bug Best Practice introduced by
The property tenancy_db_name does not exist on App\Models\Tenant. Since you implemented __get, consider adding a @property annotation.
Loading history...
38
            }
39
        }
40
//            Log::debug($value);
41
//            Log::debug('Avatar-'.$user->avatar);
42
//            Log::debug($company);
43
        session()->put('db', $value);
44
        config([$key => $value]);
45
        \DB::connection('tenantdb')->getDatabaseName();
46
        // Log::debug('DB-'.$databaseName);
47
48
        //Family::setConnection();
49
        // config(['database.default'=>'tenant']);
50
        /*}/*else {
51
            config(['database.default'=>'mysql']);
52
        }*/
53
54
        /*if ($request->has('_tenantId')) {
55
            $request->request->remove('_tenantId');
56
        }*/
57
        return $next($request);
58
    }
59
}
60