2020-05-28 17:39:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
use App\Factory\ClientContactFactory;
|
|
|
|
use App\Factory\ClientFactory;
|
2020-05-28 17:39:38 +02:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Requests\ClientPortal\RegisterRequest;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Company;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class ContactRegisterController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware(['guest', 'contact.register']);
|
|
|
|
}
|
|
|
|
|
2021-02-17 12:13:27 +01:00
|
|
|
public function showRegisterForm(string $company_key = '')
|
2020-05-28 17:39:38 +02:00
|
|
|
{
|
2021-02-17 12:13:27 +01:00
|
|
|
$key = request()->has('key') ? request('key') : $company_key;
|
2020-06-22 10:47:55 +02:00
|
|
|
|
2021-02-17 12:13:27 +01:00
|
|
|
$company = Company::where('company_key', $key)->firstOrFail();
|
|
|
|
|
|
|
|
return render('auth.register', ['company' => $company]);
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function register(RegisterRequest $request)
|
|
|
|
{
|
2020-06-22 10:26:48 +02:00
|
|
|
$request->merge(['company' => $request->company()]);
|
|
|
|
|
|
|
|
$client = $this->getClient($request->all());
|
|
|
|
$client_contact = $this->getClientContact($request->all(), $client);
|
2020-05-28 17:39:38 +02:00
|
|
|
|
2020-06-18 17:09:28 +02:00
|
|
|
Auth::guard('contact')->login($client_contact, true);
|
2020-05-28 17:39:38 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.dashboard');
|
|
|
|
}
|
2020-06-22 10:26:48 +02:00
|
|
|
|
|
|
|
private function getClient(array $data)
|
|
|
|
{
|
|
|
|
$client = ClientFactory::create($data['company']->id, $data['company']->owner()->id);
|
|
|
|
|
|
|
|
$client->fill($data);
|
|
|
|
$client->save();
|
|
|
|
|
|
|
|
return $client;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClientContact(array $data, Client $client)
|
|
|
|
{
|
|
|
|
$client_contact = ClientContactFactory::create($data['company']->id, $data['company']->owner()->id);
|
|
|
|
$client_contact->fill($data);
|
|
|
|
|
|
|
|
$client_contact->client_id = $client->id;
|
|
|
|
$client_contact->is_primary = true;
|
|
|
|
$client_contact->password = Hash::make($data['password']);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
$client_contact->save();
|
|
|
|
|
|
|
|
return $client_contact;
|
|
|
|
}
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|