Issues (185)

app/Jobs/TenantDatabase.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Models\Tenant;
6
use App\Services\TenantManager;
7
use Illuminate\Bus\Queueable;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Foundation\Bus\Dispatchable;
10
use Illuminate\Queue\InteractsWithQueue;
11
12
class TenantDatabase implements ShouldQueue
13
{
14
    use Dispatchable;
15
    use InteractsWithQueue;
16
    use Queueable;
17
18
    protected $tenant;
19
20
    protected $tenantManager;
21
22
    public function __construct(Tenant $tenant, TenantManager $tenantManager)
23
    {
24
        $this->tenant = $tenant;
25
        $this->tenantManager = $tenantManager;
26
    }
27
28
    public function handle()
29
    {
30
        $database = 'tenant_'.$this->tenant->id;
31
        $connection = \DB::connection('tenant');
32
        $createMysql = $connection->statement('CREATE DATABASE '.$database);
33
34
        if ($createMysql) {
35
            $this->tenantManager->setTenant($this->tenant);
36
            \DB::connection('tenant')->purge();
0 ignored issues
show
The method purge() does not exist on Illuminate\Database\Connection. ( Ignorable by Annotation )

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

36
            \DB::connection('tenant')->/** @scrutinizer ignore-call */ purge();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
            $this->migrate();
38
        } else {
39
            $connection->statement('DROP DATABASE '.$database);
40
        }
41
    }
42
43
    private function migrate()
44
    {
45
        $migrator = app('migrator');
46
        $migrator->setConnection('tenant');
47
48
        if (!$migrator->repositoryExists()) {
49
            $migrator->getRepository()->createRepository();
50
        }
51
52
        $migrator->run(database_path('migrations/tenants'), []);
53
    }
54
}
55