1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Jobs/Account/CreateAccount.php

80 lines
1.5 KiB
PHP
Raw Normal View History

2018-10-24 05:50:15 +02:00
<?php
namespace App\Jobs\Account;
use App\Events\Account\AccountCreated;
use App\Jobs\Company\CreateCompany;
use App\Jobs\User\CreateUser;
use App\Models\Account;
use App\Models\User;
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;
use UserSessionAttributes;
2018-10-24 05:50:15 +02:00
protected $request;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(array $request)
2018-10-24 05:50:15 +02:00
{
$this->request = $request;
}
/**
* Execute the job.
*
* @return void
*/
public function handle() : ?User
2018-10-24 05:50:15 +02:00
{
/*
* Create account
*/
$account = Account::create($this->request);
2018-10-24 05:50:15 +02:00
/*
* Create company
*/
2018-10-24 05:50:15 +02:00
$company = CreateCompany::dispatchNow($this->request, $account);
/*
* Set default company
*/
$account->default_company_id = $company->id;
$account->save();
/*
* Create user
*/
$user = CreateUser::dispatchNow($this->request, $account, $company);
2018-10-24 05:50:15 +02: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);
/*
* Fire related events
*/
event(new AccountCreated($user));
2018-10-24 05:50:15 +02:00
return $user;
}
}