RegisterController::validator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\Jobs\Tenant\CreateDB;
7
use App\Jobs\Tenant\Migration;
8
use App\Models\User;
9
use App\Person;
10
use App\Providers\RouteServiceProvider;
11
use App\Traits\ActivationTrait;
12
use App\Traits\ConnectionTrait;
13
use Illuminate\Foundation\Auth\RegistersUsers;
14
use Illuminate\Support\Facades\DB;
15
use Illuminate\Support\Facades\Hash;
16
// use LaravelEnso\Multitenancy\Jobs\CreateDatabase;
17
// use LaravelEnso\Multitenancy\Jobs\Migrate;
18
use Illuminate\Support\Facades\Validator;
19
use LaravelEnso\Companies\Models\Company;
20
use LaravelEnso\Core\Models\UserGroup;
21
use LaravelEnso\Multitenancy\Enums\Connections;
22
use LaravelEnso\Roles\Models\Role;
23
use Str;
24
25
class RegisterController extends Controller
26
{
27
    use RegistersUsers;
28
    use ActivationTrait;
29
30
    protected $redirectTo = RouteServiceProvider::HOME;
31
32
    public function __construct()
33
    {
34
        $this->middleware('guest');
35
    }
36
37
    protected function validator(array $data)
38
    {
39
        return Validator::make($data, [
40
            'name' => ['required', 'string', 'max:255'],
41
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
42
            'password' => ['required', 'string', 'min:5', 'confirmed'],
43
        ]);
44
    }
45
46
    protected function create(array $data)
47
    {
48
        try {
49
            // DB::beginTransaction();
50
            // create person
51
            $person = new Person();
52
            $person->name = $data['name'];
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
53
            $person->email = $data['email'];
0 ignored issues
show
Bug introduced by
The property email does not seem to exist on App\Person. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
54
            $person->save();
55
56
            // get user_group_id
57
            $user_group = UserGroup::where('name', 'Administrators')->first();
58
            if ($user_group == null) {
59
                // create user_group
60
                $user_group = UserGroup::create(['name'=>'Administrators', 'description'=>'Administrator users group']);
61
            }
62
63
            // get role_id
64
            $role = Role::where('name', 'supervisor')->first();
65
            if ($role == null) {
66
                $role = Role::create(['menu_id'=>1, 'name'=>'supervisor', 'display_name'=>'Supervisor', 'description'=>'Supervisor role.']);
67
            }
68
            $user = User::create([
69
                'email' => $data['email'],
70
                'password' => Hash::make($data['password']),
71
                'person_id' => $person->id,
72
                'group_id' => $user_group->id,
73
                'role_id' => $role->id,
74
                'is_active' => 1,
75
            ]);
76
            // send verification email;
77
78
            $this->initiateEmailActivation($user);
79
80
            $company = Company::create([
81
                'name' => $data['name'],
82
                'email' => $data['email'],
83
                // 'is_active' => 1,
84
                'is_tenant' => 1,
85
                'status' => 1,
86
            ]);
87
88
//          $company->attachPerson($person->id, 'Owner');
89
            // DB::commit();
90
91
            $person->companies()->attach($company->id, ['person_id' => $person->id, 'is_main' => 1, 'is_mandatary' => 1, 'company_id' => $company->id]);
92
93
            // Dispatch Tenancy Jobs
94
95
            CreateDB::dispatch($company);
96
            Migration::dispatch($company, $data['name'], $data['email'], $data['password']);
97
98
            return $user;
99
        } catch (\Exception $e) {
100
            // DB::rollBack();
101
            throw $e;
102
        }
103
    }
104
}
105