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

Tenant   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 9 1
A tenantId() 0 3 1
A get() 0 3 1
A tenantDatabase() 0 3 1
A tenantPrefix() 0 7 2
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