Passed
Push — master ( 51edae...d29a9b )
by Curtis
11:52 queued 05:54
created

Migration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 7
rs 10
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 Illuminate\Support\Facades\Hash;
15
use LaravelEnso\Companies\Models\Company;
16
use LaravelEnso\Core\Models\UserGroup;
17
use LaravelEnso\Multitenancy\Enums\Connections;
18
use LaravelEnso\Multitenancy\Services\Tenant;
19
use LaravelEnso\Roles\Models\Role;
20
use Str;
21
22
class Migration implements ShouldQueue
23
{
24
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\Tenant\Migration: $id, $relations, $class, $keyBy
Loading history...
25
    private $tenant;
26
    private $name;
27
    private $email;
28
    private $password;
29
30
    /**
31
     * Create a new job instance.
32
     *
33
     * @return void
34
     */
35
    public function __construct(Company $tenant, $name = '', $email = '', $password = '')
36
    {
37
        //
38
        $this->tenant = $tenant;
39
        $this->name = $name;
40
        $this->email = $email;
41
        $this->password = $password;
42
        // $this->queue = 'sync';
43
    }
44
45
    /**
46
     * Execute the job.
47
     *
48
     * @return void
49
     */
50
    public function handle()
51
    {
52
        //
53
54
        Tenant::set($this->tenant);
55
        $company = Tenant::get();
56
        $db = Connections::Tenant.$company->id;
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
57
        Artisan::call('migrate', [
58
            '--database' => Connections::Tenant,
59
            '--path' => '/database/migrations/tenant',
60
            '--force' => true,
61
        ]);
62
        Artisan::call('db:seed', [
63
            '--database' => Connections::Tenant,
64
            '--force' => true,
65
        ]);
66
67
        $person = DB::connection(Connections::Tenant)->table('people')->insert([
68
            'email'=>$this->email,
69
            'name' => $this->name,
70
        ]);
71
        // get user_group_id
72
        $user_group = 1;
73
74
        // get role_id
75
        $role = 1;
76
77
        $person = DB::connection(Connections::Tenant)->table('users')->insert([
0 ignored issues
show
Unused Code introduced by
The assignment to $person is dead and can be removed.
Loading history...
78
            'email' => $this->email,
79
            'password' => Hash::make($this->password),
80
            'person_id' => $person,
81
            'group_id' => $user_group,
82
            'role_id' => $role,
83
            'is_active' => 1,
84
        ]);
85
    }
86
}
87