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

101 lines
2.4 KiB
PHP
Raw Normal View History

2018-10-24 05:50:15 +02:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
2018-10-24 05:50:15 +02:00
namespace App\Jobs\Account;
use App\Events\Account\AccountCreated;
use App\Jobs\Company\CreateCompany;
2019-03-26 05:46:08 +01:00
use App\Jobs\Company\CreateCompanyToken;
use App\Jobs\User\CreateUser;
use App\Models\Account;
use App\Models\User;
use App\Notifications\NewAccountCreated;
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;
2019-03-28 10:05:13 +01:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
2018-10-24 05:50:15 +02:00
class CreateAccount
{
use Dispatchable;
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
*/
2019-04-18 13:57:22 +02:00
public function handle() : ?Account
2018-10-24 05:50:15 +02:00
{
/*
* Create account
*/
$account = Account::create($this->request);
/*
* Create company
*/
2018-10-24 05:50:15 +02:00
$company = CreateCompany::dispatchNow($this->request, $account);
2019-09-20 07:13:58 +02:00
$company->load('account');
/*
* Set default company
*/
$account->default_company_id = $company->id;
$account->save();
/*
* Create user
*/
2019-04-18 13:57:22 +02:00
$user = CreateUser::dispatchNow($this->request, $account, $company, true); //make user company_owner
2019-06-24 13:05:47 +02:00
/*
* Required dependencies
*/
if ($user) {
auth()->login($user, false);
}
2019-06-24 13:05:47 +02:00
$user->setCompany($company);
2019-03-26 05:46:08 +01:00
/*
* Create token
*/
$user_agent = isset($this->request['token_name']) ? $this->request['token_name'] : request()->server('HTTP_USER_AGENT');
$company_token = CreateCompanyToken::dispatchNow($company, $user, $user_agent);
/*
* Fire related events
*/
if ($user) {
2019-07-08 07:54:46 +02:00
event(new AccountCreated($user));
}
$user->fresh();
Notification::route('slack', config('ninja.notification.slack'))
->notify(new NewAccountCreated($user, $company));
2019-04-18 13:57:22 +02:00
return $account;
2018-10-24 05:50:15 +02:00
}
}