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

Tenant::tenantId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace App\Service;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Support\Str;
7
use App\Models\enso\companies\Company;
8
use LaravelEnso\Multitenancy\Enums\Connections;
9
10
class Tenant
11
{
12
    private static $tenantPrefix;
13
14
    public static function set(Company $company)
15
    {
16
        $key = 'database.connections.'.Connections::Tenant.'.database';
17
        $value = self::tenantPrefix().$company->id;
18
        config([$key => $value]);
19
20
        DB::purge(Connections::Tenant);
21
22
        DB::reconnect(Connections::Tenant);
23
    }
24
25
    public static function get()
26
    {
27
        return Company::find(self::tenantId());
28
    }
29
30
    public static function tenantDatabase()
31
    {
32
        return config('database.connections.'.Connections::Tenant.'.database');
33
    }
34
35
    private static function tenantId()
36
    {
37
        return (int) Str::replaceFirst(Connections::Tenant, '', self::tenantDatabase());
38
    }
39
40
    private static function tenantPrefix()
41
    {
42
        if (! isset(self::$tenantPrefix)) {
43
            self::$tenantPrefix = self::tenantDatabase();
44
        }
45
46
        return self::$tenantPrefix;
47
    }
48
}
49