added pointer interfaces for jobs

This commit is contained in:
Denis Duliçi
2021-09-06 11:53:57 +03:00
parent abd55133f1
commit c08a8cfc4e
63 changed files with 635 additions and 1524 deletions

View File

@@ -5,57 +5,39 @@ namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Events\Common\CompanyCreated;
use App\Events\Common\CompanyCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\Company;
use Artisan;
use Illuminate\Support\Facades\Artisan;
class CreateCompany extends Job
class CreateCompany extends Job implements HasOwner, ShouldCreate
{
protected $company;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
public function handle(): Company
{
$current_company_id = company_id();
event(new CompanyCreating($this->request));
\DB::transaction(function () {
$this->company = Company::create($this->request->all());
$this->model = Company::create($this->request->all());
$this->company->makeCurrent();
$this->model->makeCurrent();
$this->callSeeds();
$this->updateSettings();
});
event(new CompanyCreated($this->company));
event(new CompanyCreated($this->model));
if (!empty($current_company_id)) {
company($current_company_id)->makeCurrent();
}
return $this->company;
return $this->model;
}
protected function callSeeds()
protected function callSeeds(): void
{
// Set custom locale
if ($this->request->has('locale')) {
@@ -64,7 +46,7 @@ class CreateCompany extends Job
// Company seeds
Artisan::call('company:seed', [
'company' => $this->company->id
'company' => $this->model->id
]);
if (!$user = user()) {
@@ -72,22 +54,22 @@ class CreateCompany extends Job
}
// Attach company to user logged in
$user->companies()->attach($this->company->id);
$user->companies()->attach($this->model->id);
// User seeds
Artisan::call('user:seed', [
'user' => $user->id,
'company' => $this->company->id,
'company' => $this->model->id,
]);
}
protected function updateSettings()
protected function updateSettings(): void
{
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->model->id);
if ($company_logo) {
$this->company->attachMedia($company_logo, 'company_logo');
$this->model->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}

View File

@@ -3,54 +3,36 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Auth\User;
use App\Models\Auth\Role;
use App\Models\Common\Contact;
use Illuminate\Support\Str;
class CreateContact extends Job
class CreateContact extends Job implements HasOwner, ShouldCreate
{
protected $contact;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Contact
*/
public function handle()
public function handle(): Contact
{
\DB::transaction(function () {
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
}
$this->contact = Contact::create($this->request->all());
$this->model = Contact::create($this->request->all());
// Upload logo
if ($this->request->file('logo')) {
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type));
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type));
$this->contact->attachMedia($media, 'logo');
$this->model->attachMedia($media, 'logo');
}
});
return $this->contact;
return $this->model;
}
public function createUser()
public function createUser(): void
{
// Check if user exist
if ($user = User::where('email', $this->request['email'])->first()) {

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Auth\User;
use App\Models\Common\Company;
use App\Models\Common\Dashboard;
@@ -10,29 +12,9 @@ use App\Models\Common\Widget;
use App\Utilities\Widgets;
use Illuminate\Support\Arr;
class CreateDashboard extends Job
class CreateDashboard extends Job implements HasOwner, ShouldCreate
{
protected $dashboard;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
public function handle(): Dashboard
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;
@@ -43,17 +25,17 @@ class CreateDashboard extends Job
return;
}
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
$this->model = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
$this->dashboard->users()->attach($users);
$this->model->users()->attach($users);
$this->checkAndCreateWidgets();
});
return $this->dashboard;
return $this->model;
}
protected function getUsers()
protected function getUsers(): array
{
$list = [];
@@ -88,7 +70,7 @@ class CreateDashboard extends Job
return $list;
}
protected function shouldCreateDashboardFor($user)
protected function shouldCreateDashboardFor($user): bool
{
if (empty($user)) {
return false;
@@ -102,7 +84,7 @@ class CreateDashboard extends Job
return true;
}
protected function checkAndCreateWidgets()
protected function checkAndCreateWidgets(): void
{
$sort = 1;
@@ -119,7 +101,7 @@ class CreateDashboard extends Job
}
}
protected function createWidgets($widgets, &$sort)
protected function createWidgets($widgets, &$sort): void
{
foreach ($widgets as $class => $name) {
// It's just an array of classes
@@ -129,14 +111,14 @@ class CreateDashboard extends Job
}
Widget::firstOrCreate([
'company_id' => $this->dashboard->company_id,
'dashboard_id' => $this->dashboard->id,
'company_id' => $this->model->company_id,
'dashboard_id' => $this->model->id,
'class' => $class,
], [
'name' => $name,
'sort' => $sort,
'settings' => (new $class())->getDefaultSettings(),
'created_by' => $this->dashboard->created_by,
'created_by' => $this->model->created_by,
]);
$sort++;

View File

@@ -3,46 +3,28 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Jobs\Common\CreateItemTaxes;
use App\Models\Common\Item;
class CreateItem extends Job
class CreateItem extends Job implements HasOwner, ShouldCreate
{
protected $item;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
public function handle(): Item
{
\DB::transaction(function () {
$this->item = Item::create($this->request->all());
$this->model = Item::create($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture');
$this->model->attachMedia($media, 'picture');
}
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
$this->dispatch(new CreateItemTaxes($this->model, $this->request));
});
return $this->item;
return $this->model;
}
}

View File

@@ -3,30 +3,29 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\Item;
use App\Models\Common\ItemTax;
class CreateItemTaxes extends Job
class CreateItemTaxes extends Job implements ShouldCreate
{
protected $item;
protected $request;
/**
* Create a new job instance.
*
* @param $item
* @param $request
*/
public function __construct($item, $request)
public function __construct(Item $item, $request)
{
$this->item = $item;
$this->request = $request;
parent::__construct($item, $request);
}
/**
* Execute the job.
*
* @return mixed
* @todo type hint after upgrading to PHP 8
*/
public function handle()
{

View File

@@ -3,36 +3,18 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\Report;
class CreateReport extends Job
class CreateReport extends Job implements HasOwner, ShouldCreate
{
protected $report;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Report
*/
public function handle()
public function handle(): Report
{
\DB::transaction(function () {
$this->report = Report::create($this->request->all());
$this->model = Report::create($this->request->all());
});
return $this->report;
return $this->model;
}
}

View File

@@ -3,31 +3,13 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\Widget;
class CreateWidget extends Job
class CreateWidget extends Job implements HasOwner, ShouldCreate
{
protected $widget;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
$this->request->merge(['created_by' => user_id()]);
}
/**
* Execute the job.
*
* @return Widget
*/
public function handle()
public function handle(): Widget
{
$this->request['enabled'] = $this->request['enabled'] ?? 1;

View File

@@ -5,53 +5,41 @@ namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Events\Common\CompanyDeleted;
use App\Events\Common\CompanyDeleting;
use App\Interfaces\Job\ShouldDelete;
use App\Traits\Users;
class DeleteCompany extends Job
class DeleteCompany extends Job implements ShouldDelete
{
use Users;
protected $company;
protected $current_company_id;
/**
* Create a new job instance.
*
* @param $company
*/
public function __construct($company)
public function booted(...$arguments): void
{
$this->company = $company;
$this->current_company_id = company_id();
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle(): bool
{
$this->authorize();
$this->company->makeCurrent();
$this->model->makeCurrent();
event(new CompanyDeleting($this->company, $this->current_company_id));
event(new CompanyDeleting($this->model, $this->current_company_id));
\DB::transaction(function () {
$this->deleteRelationships($this->company, [
$this->deleteRelationships($this->model, [
'accounts', 'document_histories', 'document_item_taxes', 'document_items', 'document_totals', 'documents', 'categories',
'contacts', 'currencies', 'dashboards', 'email_templates', 'items', 'module_histories', 'modules', 'reconciliations',
'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
]);
$this->company->users()->detach();
$this->model->users()->detach();
$this->company->delete();
$this->model->delete();
});
event(new CompanyDeleted($this->company, $this->current_company_id));
event(new CompanyDeleted($this->model, $this->current_company_id));
company($this->current_company_id)->makeCurrent();
@@ -60,20 +48,18 @@ class DeleteCompany extends Job
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
// Can't delete active company
if ($this->company->id == $this->current_company_id) {
if ($this->model->id == $this->current_company_id) {
$message = trans('companies.error.delete_active');
throw new \Exception($message);
}
// Check if user can access company
if ($this->isNotUserCompany($this->company->id)) {
if ($this->isNotUserCompany($this->model->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);

View File

@@ -3,40 +3,24 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
use App\Jobs\Auth\DeleteUser;
use App\Traits\Contacts;
class DeleteContact extends Job
class DeleteContact extends Job implements ShouldDelete
{
use Contacts;
protected $contact;
/**
* Create a new job instance.
*
* @param $contact
*/
public function __construct($contact)
{
$this->contact = $contact;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle() :bool
{
$this->authorize();
\DB::transaction(function () {
if ($user = $this->contact->user) {
if ($user = $this->model->user) {
$this->dispatch(new DeleteUser($user));
}
$this->contact->delete();
$this->model->delete();
});
return true;
@@ -44,19 +28,17 @@ class DeleteContact extends Job
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
if ($relationships = $this->getRelationships()) {
$message = trans('messages.warning.deleted', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]);
$message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
public function getRelationships(): array
{
$rels = [
'transactions' => 'transactions',
@@ -68,6 +50,6 @@ class DeleteContact extends Job
$rels['bills'] = 'bills';
}
return $this->countRelationships($this->contact, $rels);
return $this->countRelationships($this->model, $rels);
}
}

View File

@@ -5,39 +5,23 @@ namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Exceptions\Common\LastDashboard;
use App\Exceptions\Common\NotUserDashboard;
use App\Interfaces\Job\ShouldDelete;
use App\Traits\Users;
class DeleteDashboard extends Job
class DeleteDashboard extends Job implements ShouldDelete
{
use Users;
protected $dashboard;
/**
* Create a new job instance.
*
* @param $dashboard
*/
public function __construct($dashboard)
{
$this->dashboard = $dashboard;
}
/**
* Execute the job.
*
* @return boolean
*/
public function handle()
public function handle(): bool
{
$this->authorize();
\DB::transaction(function () {
$this->deleteRelationships($this->dashboard, ['widgets']);
$this->deleteRelationships($this->model, ['widgets']);
$this->dashboard->users()->detach();
$this->model->users()->detach();
$this->dashboard->delete();
$this->model->delete();
});
return true;
@@ -45,13 +29,11 @@ class DeleteDashboard extends Job
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
// Can't delete last dashboard for any shared user
foreach ($this->dashboard->users as $user) {
foreach ($this->model->users as $user) {
if ($user->dashboards()->enabled()->count() > 1) {
continue;
}
@@ -62,7 +44,7 @@ class DeleteDashboard extends Job
}
// Check if user can access dashboard
if ($this->isNotUserDashboard($this->dashboard->id)) {
if ($this->isNotUserDashboard($this->model->id)) {
$message = trans('dashboards.error.not_user_dashboard');
throw new NotUserDashboard($message);

View File

@@ -3,34 +3,18 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
class DeleteItem extends Job
class DeleteItem extends Job implements ShouldDelete
{
protected $item;
/**
* Create a new job instance.
*
* @param $item
*/
public function __construct($item)
{
$this->item = $item;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle(): bool
{
$this->authorize();
\DB::transaction(function () {
$this->deleteRelationships($this->item, ['taxes']);
$this->deleteRelationships($this->model, ['taxes']);
$this->item->delete();
$this->model->delete();
});
return true;
@@ -38,25 +22,23 @@ class DeleteItem extends Job
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
if ($relationships = $this->getRelationships()) {
$message = trans('messages.warning.deleted', ['name' => $this->item->name, 'text' => implode(', ', $relationships)]);
$message = trans('messages.warning.deleted', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
public function getRelationships(): array
{
$rels = [
'invoice_items' => 'invoices',
'bill_items' => 'bills',
];
return $this->countRelationships($this->item, $rels);
return $this->countRelationships($this->model, $rels);
}
}

View File

@@ -3,30 +3,14 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
class DeleteReport extends Job
class DeleteReport extends Job implements ShouldDelete
{
protected $report;
/**
* Create a new job instance.
*
* @param $report
*/
public function __construct($report)
{
$this->report = $report;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle(): bool
{
\DB::transaction(function () {
$this->report->delete();
$this->model->delete();
});
return true;

View File

@@ -3,30 +3,14 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
class DeleteWidget extends Job
class DeleteWidget extends Job implements ShouldDelete
{
protected $widget;
/**
* Create a new job instance.
*
* @param $widget
*/
public function __construct($widget)
{
$this->widget = $widget;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
public function handle(): bool
{
\DB::transaction(function () {
$this->widget->delete();
$this->model->delete();
});
return true;

View File

@@ -5,47 +5,31 @@ namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Events\Common\CompanyUpdated;
use App\Events\Common\CompanyUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Common\Company;
use App\Traits\Users;
class UpdateCompany extends Job
class UpdateCompany extends Job implements ShouldUpdate
{
use Users;
protected $company;
protected $request;
protected $current_company_id;
/**
* Create a new job instance.
*
* @param $company
* @param $request
*/
public function __construct($company, $request)
public function booted(...$arguments): void
{
$this->company = $company;
$this->request = $this->getRequestInstance($request);
$this->current_company_id = company_id();
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
public function handle(): Company
{
$this->authorize();
event(new CompanyUpdating($this->company, $this->request));
event(new CompanyUpdating($this->model, $this->request));
\DB::transaction(function () {
$this->company->update($this->request->all());
$this->model->update($this->request->all());
$this->company->makeCurrent();
$this->model->makeCurrent();
if ($this->request->has('name')) {
setting()->set('company.name', $this->request->get('name'));
@@ -92,10 +76,10 @@ class UpdateCompany extends Job
}
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->model->id);
if ($company_logo) {
$this->company->attachMedia($company_logo, 'company_logo');
$this->model->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}
@@ -104,31 +88,29 @@ class UpdateCompany extends Job
setting()->save();
});
event(new CompanyUpdated($this->company, $this->request));
event(new CompanyUpdated($this->model, $this->request));
if (!empty($this->current_company_id)) {
company($this->current_company_id)->makeCurrent();
}
return $this->company;
return $this->model;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
// Can't disable active company
if (($this->request->get('enabled', 1) == 0) && ($this->company->id == $this->current_company_id)) {
if (($this->request->get('enabled', 1) == 0) && ($this->model->id == $this->current_company_id)) {
$message = trans('companies.error.disable_active');
throw new \Exception($message);
}
// Check if user can access company
if ($this->isNotUserCompany($this->company->id)) {
if ($this->isNotUserCompany($this->model->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);

View File

@@ -3,73 +3,51 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\Role;
use App\Models\Auth\User;
use App\Models\Common\Contact;
use Illuminate\Support\Str;
class UpdateContact extends Job
class UpdateContact extends Job implements ShouldUpdate
{
protected $contact;
protected $request;
/**
* Create a new job instance.
*
* @param $contact
* @param $request
*/
public function __construct($contact, $request)
{
$this->contact = $contact;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Contact
*/
public function handle()
public function handle(): Contact
{
$this->authorize();
\DB::transaction(function () {
if ($this->request->get('create_user', 'false') === 'true') {
$this->createUser();
} elseif ($this->contact->user) {
$this->contact->user->update($this->request->all());
} elseif ($this->model->user) {
$this->model->user->update($this->request->all());
}
// Upload logo
if ($this->request->file('logo')) {
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->contact->type));
$media = $this->getMedia($this->request->file('logo'), Str::plural($this->model->type));
$this->contact->attachMedia($media, 'logo');
$this->model->attachMedia($media, 'logo');
}
$this->contact->update($this->request->all());
$this->model->update($this->request->all());
});
return $this->contact;
return $this->model;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
if (($this->request['enabled'] == 0) && ($relationships = $this->getRelationships())) {
$message = trans('messages.warning.disabled', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]);
$message = trans('messages.warning.disabled', ['name' => $this->model->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function createUser()
public function createUser(): void
{
// Check if user exist
if ($user = User::where('email', $this->request['email'])->first()) {
@@ -92,18 +70,18 @@ class UpdateContact extends Job
$this->request['user_id'] = $user->id;
}
public function getRelationships()
public function getRelationships(): array
{
$rels = [
'transactions' => 'transactions',
];
if ($this->contact->type == 'customer') {
if ($this->model->type == 'customer') {
$rels['invoices'] = 'invoices';
} else {
$rels['bills'] = 'bills';
}
return $this->countRelationships($this->contact, $rels);
return $this->countRelationships($this->model, $rels);
}
}

View File

@@ -3,59 +3,37 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Common\Dashboard;
use App\Traits\Users;
class UpdateDashboard extends Job
class UpdateDashboard extends Job implements ShouldUpdate
{
use Users;
protected $dashboard;
protected $request;
/**
* Create a new job instance.
*
* @param $dashboard
* @param $request
*/
public function __construct($dashboard, $request)
{
$this->dashboard = $dashboard;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Dashboard
*/
public function handle()
public function handle(): Dashboard
{
$this->authorize();
\DB::transaction(function () {
$this->dashboard->update($this->request->all());
$this->model->update($this->request->all());
if ($this->request->has('users')) {
$this->dashboard->users()->sync($this->request->get('users'));
$this->model->users()->sync($this->request->get('users'));
}
});
return $this->dashboard;
return $this->model;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
public function authorize(): void
{
// Can't disable last dashboard for any shared user
if ($this->request->has('enabled') && !$this->request->get('enabled')) {
foreach ($this->dashboard->users as $user) {
foreach ($this->model->users as $user) {
if ($user->dashboards()->enabled()->count() > 1) {
continue;
}
@@ -77,7 +55,7 @@ class UpdateDashboard extends Job
}
// Check if user can access dashboard
if ($this->isNotUserDashboard($this->dashboard->id)) {
if ($this->isNotUserDashboard($this->model->id)) {
$message = trans('dashboards.error.not_user_dashboard');
throw new \Exception($message);

View File

@@ -3,49 +3,29 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Jobs\Common\CreateItemTaxes;
use App\Models\Common\Item;
class UpdateItem extends Job
class UpdateItem extends Job implements ShouldUpdate
{
protected $item;
protected $request;
/**
* Create a new job instance.
*
* @param $item
* @param $request
*/
public function __construct($item, $request)
{
$this->item = $item;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
public function handle(): Item
{
\DB::transaction(function () {
$this->item->update($this->request->all());
$this->model->update($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture');
$this->model->attachMedia($media, 'picture');
}
$this->deleteRelationships($this->item, ['taxes']);
$this->deleteRelationships($this->model, ['taxes']);
$this->dispatch(new CreateItemTaxes($this->item, $this->request));
$this->dispatch(new CreateItemTaxes($this->model, $this->request));
});
return $this->item;
return $this->model;
}
}

View File

@@ -3,37 +3,17 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Common\Report;
class UpdateReport extends Job
class UpdateReport extends Job implements ShouldUpdate
{
protected $report;
protected $request;
/**
* Create a new job instance.
*
* @param $report
* @param $request
*/
public function __construct($report, $request)
{
$this->report = $report;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Report
*/
public function handle()
public function handle(): Report
{
\DB::transaction(function () {
$this->report->update($this->request->all());
$this->model->update($this->request->all());
});
return $this->report;
return $this->model;
}
}

View File

@@ -3,37 +3,17 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Common\Widget;
class UpdateWidget extends Job
class UpdateWidget extends Job implements ShouldUpdate
{
protected $widget;
protected $request;
/**
* Create a new job instance.
*
* @param $widget
* @param $request
*/
public function __construct($widget, $request)
{
$this->widget = $widget;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
public function handle(): Widget
{
\DB::transaction(function () {
$this->widget->update($this->request->all());
$this->model->update($this->request->all());
});
return $this->widget;
return $this->model;
}
}