Passed
Push — master ( 7c751b...4c7ed3 )
by Curtis
05:20 queued 11s
created

Migration::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 34
rs 9.568
c 1
b 0
f 0
1
<?php
2
3
namespace App\Jobs\Tenant;
4
5
use App\Models\User;
6
use App\Person;
7
use DB;
8
use Illuminate\Bus\Queueable;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
use Illuminate\Foundation\Bus\Dispatchable;
11
use Illuminate\Queue\InteractsWithQueue;
12
use Illuminate\Queue\SerializesModels;
13
use Illuminate\Support\Facades\Artisan;
14
use LaravelEnso\Companies\Models\Company;
15
use LaravelEnso\Multitenancy\Enums\Connections;
16
use LaravelEnso\Multitenancy\Services\Tenant;
17
use App\Models\User;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use App\Models\User as User because the name is already in use
Loading history...
18
use App\Person;
19
use LaravelEnso\Roles\Models\Role;
20
use LaravelEnso\Core\Models\UserGroup;
21
use Illuminate\Support\Facades\Hash;
22
use DB;
23
use Str;
24
25
class Migration implements ShouldQueue
26
{
27
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
28
    private $tenant;
29
    private $name;
30
    private $email;
31
    private $password;
32
33
    /**
34
     * Create a new job instance.
35
     *
36
     * @return void
37
     */
38
    public function __construct(Company $tenant, $name = '', $email = '', $password = '')
39
    {
40
        //
41
        $this->tenant = $tenant;
42
        $this->name = $name;
43
        $this->email = $email;
44
        $this->password = $password;
45
        // $this->queue = 'sync';
46
    }
47
48
    /**
49
     * Execute the job.
50
     *
51
     * @return void
52
     */
53
    public function handle()
54
    {
55
        //
56
57
        Tenant::set($this->tenant);
58
        $company = Tenant::get();
59
        $db = Connections::Tenant.$company->id;
60
        Artisan::call('migrate', [
61
            '--database' => Connections::Tenant,
62
            '--path' => '/database/migrations/tenant',
63
            '--force' => true,
64
        ]);
65
        Artisan::call('db:seed', [
66
            '--database' => Connections::Tenant,
67
            '--force' => true,
68
        ]);
69
70
        $person = DB::connection(Connections::Tenant)->table('people')->insert([
71
            'email'=>$this->email,
72
            'name' => $this->name,
73
        ]);
74
        // get user_group_id
75
        $user_group = 1;
76
77
        // get role_id
78
        $role = 1;
79
80
        $person = DB::connection(Connections::Tenant)->table('users')->insert([
81
            'email' => $this->email,
82
            'password' => Hash::make($this->password),
83
            'person_id' => $person,
84
            'group_id' => $user_group,
85
            'role_id' => $role,
86
            'is_active' => 1,
87
        ]);
88
    }
89
}
90