1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers\Auth;
|
---|
4 |
|
---|
5 | use App\Http\Controllers\Controller;
|
---|
6 | use App\Http\Controllers\Utils\UtilController;
|
---|
7 | use App\Models\Artist;
|
---|
8 | use App\Models\Organizer;
|
---|
9 | use App\Providers\RouteServiceProvider;
|
---|
10 | use App\Models\User;
|
---|
11 | use Illuminate\Foundation\Auth\RegistersUsers;
|
---|
12 | use Illuminate\Http\Request;
|
---|
13 | use Illuminate\Support\Facades\Hash;
|
---|
14 | use Illuminate\Support\Facades\Log;
|
---|
15 | use Illuminate\Support\Facades\Validator;
|
---|
16 | use Laravolt\Avatar\Avatar;
|
---|
17 | use Exception;
|
---|
18 |
|
---|
19 | class RegisterController extends Controller
|
---|
20 | {
|
---|
21 | /*
|
---|
22 | |--------------------------------------------------------------------------
|
---|
23 | | Register Controller
|
---|
24 | |--------------------------------------------------------------------------
|
---|
25 | |
|
---|
26 | | This controller handles the registration of new users as well as their
|
---|
27 | | validation and creation. By default this controller uses a trait to
|
---|
28 | | provide this functionality without requiring any additional code.
|
---|
29 | |
|
---|
30 | */
|
---|
31 |
|
---|
32 | use RegistersUsers;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Where to redirect users after registration.
|
---|
36 | *
|
---|
37 | * @var string
|
---|
38 | */
|
---|
39 | protected $redirectTo = RouteServiceProvider::HOME;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Create a new controller instance.
|
---|
43 | *
|
---|
44 | * @return void
|
---|
45 | */
|
---|
46 | public function __construct()
|
---|
47 | {
|
---|
48 | $this->middleware('guest');
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Get a validator for an incoming registration request.
|
---|
53 | *
|
---|
54 | * @param array $data
|
---|
55 | * @return \Illuminate\Contracts\Validation\Validator
|
---|
56 | */
|
---|
57 | protected function validator(array $data)
|
---|
58 | {
|
---|
59 | return Validator::make($data, [
|
---|
60 | 'name' => ['required', 'string', 'max:80'],
|
---|
61 | 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
|
---|
62 | 'password' => ['required', 'string', 'min:8', 'confirmed'],
|
---|
63 | 'account_type' => ['required', 'string', 'regex:(artist|organizer)'],
|
---|
64 | ]);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Create a new user instance after a valid registration.
|
---|
69 | *
|
---|
70 | * @param array $data
|
---|
71 | * @return User|void
|
---|
72 | */
|
---|
73 | protected function create(array $data)
|
---|
74 | {
|
---|
75 | $username = strtolower(preg_replace('/\s+/', '', $data['name'])) . uniqid();
|
---|
76 | $image_name = ($username . '-' . uniqid() . '.jpg');
|
---|
77 |
|
---|
78 | $user = User::create([
|
---|
79 | 'name' => $data['name'],
|
---|
80 | 'email' => $data['email'],
|
---|
81 | 'password' => Hash::make($data['password']),
|
---|
82 | 'username' => $username,
|
---|
83 | 'profile_picture' => $image_name,
|
---|
84 | 'type' => strtoupper($data['account_type']),
|
---|
85 | ]);
|
---|
86 |
|
---|
87 | switch(strtolower($data['account_type'])) {
|
---|
88 | case 'artist':
|
---|
89 | $artist = Artist::create([
|
---|
90 | 'user_id' => $user->id
|
---|
91 | ]);
|
---|
92 |
|
---|
93 | $artist->user()->associate($user);
|
---|
94 |
|
---|
95 | try {
|
---|
96 | $artist->save();
|
---|
97 | } catch (Exception $e) {
|
---|
98 | Log::error($e->getMessage());
|
---|
99 | }
|
---|
100 | break;
|
---|
101 | case 'organizer':
|
---|
102 | $organizer = Organizer::create([
|
---|
103 | 'user_id' => $user->id
|
---|
104 | ]);
|
---|
105 |
|
---|
106 | $organizer->user()->associate($user);
|
---|
107 |
|
---|
108 | try {
|
---|
109 | $organizer->save();
|
---|
110 | } catch (Exception $e) {
|
---|
111 | Log::error($e->getMessage());
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | UtilController::saveAvatarImage($image_name, $data['name']);
|
---|
116 |
|
---|
117 | return $user;
|
---|
118 | }
|
---|
119 | }
|
---|