Tenant   A
last analyzed

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 App\Models\enso\companies\Company;
0 ignored issues
show
Bug introduced by
The type App\Models\enso\companies\Company was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Str;
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