2018-10-24 05:50:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Account;
|
|
|
|
|
|
|
|
use App\Events\Account\AccountCreated;
|
|
|
|
use App\Jobs\Company\CreateCompany;
|
2018-12-02 11:42:06 +01:00
|
|
|
use App\Jobs\User\CreateUser;
|
|
|
|
use App\Models\Account;
|
|
|
|
use App\Models\User;
|
2018-11-03 02:01:40 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-10-24 05:50:15 +02:00
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class CreateAccount
|
|
|
|
{
|
|
|
|
|
|
|
|
use Dispatchable;
|
2018-11-03 02:01:40 +01:00
|
|
|
use UserSessionAttributes;
|
2018-10-24 05:50:15 +02:00
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
|
2018-12-02 11:42:06 +01:00
|
|
|
public function __construct(array $request)
|
2018-10-24 05:50:15 +02:00
|
|
|
{
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-27 08:24:26 +01:00
|
|
|
public function handle() : ?User
|
2018-10-24 05:50:15 +02:00
|
|
|
{
|
2018-11-03 02:01:40 +01:00
|
|
|
/*
|
|
|
|
* Create account
|
|
|
|
*/
|
2018-12-02 11:42:06 +01:00
|
|
|
$account = Account::create($this->request);
|
2018-10-24 05:50:15 +02:00
|
|
|
|
2018-11-03 02:01:40 +01:00
|
|
|
/*
|
|
|
|
* Create company
|
|
|
|
*/
|
2018-10-24 05:50:15 +02:00
|
|
|
$company = CreateCompany::dispatchNow($this->request, $account);
|
|
|
|
|
2018-11-03 02:01:40 +01:00
|
|
|
/*
|
|
|
|
* Set default company
|
|
|
|
*/
|
|
|
|
$account->default_company_id = $company->id;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create user
|
|
|
|
*/
|
2018-10-24 12:24:09 +02:00
|
|
|
$user = CreateUser::dispatchNow($this->request, $account, $company);
|
2018-10-24 05:50:15 +02:00
|
|
|
|
2018-11-03 02:01:40 +01:00
|
|
|
/*
|
|
|
|
* Set current company
|
|
|
|
*/
|
|
|
|
$this->setCurrentCompanyId($user->companies()->first()->account->default_company_id);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Login user
|
|
|
|
*/
|
2018-10-24 05:50:15 +02:00
|
|
|
Auth::loginUsingId($user->id, true);
|
|
|
|
|
2018-11-03 02:01:40 +01:00
|
|
|
/*
|
|
|
|
* Fire related events
|
|
|
|
*/
|
2018-10-24 12:24:09 +02:00
|
|
|
event(new AccountCreated($user));
|
2018-10-24 05:50:15 +02:00
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
}
|