Issues (172)

app/Service/Tenant.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Service;
4
5
use App\Models\enso\companies\Company;
0 ignored issues
show
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