Skip to content

Commit

Permalink
added item events
Browse files Browse the repository at this point in the history
  • Loading branch information
CihanSenturk committed Oct 31, 2023
1 parent 67969d0 commit 6af4c75
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/Events/Common/ItemCreated.php
@@ -0,0 +1,21 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;
use App\Models\Common\Item;

class ItemCreated extends Event
{
public $item;

/**
* Create a new event instance.
*
* @param $item
*/
public function __construct(Item $item)
{
$this->item = $item;
}
}
20 changes: 20 additions & 0 deletions app/Events/Common/ItemCreating.php
@@ -0,0 +1,20 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;

class ItemCreating extends Event
{
public $request;

/**
* Create a new event instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $request;
}
}
20 changes: 20 additions & 0 deletions app/Events/Common/ItemDeleted.php
@@ -0,0 +1,20 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;

class ItemDeleted extends Event
{
public $item;

/**
* Create a new event instance.
*
* @param $item
*/
public function __construct($item)
{
$this->item = $item;
}
}
20 changes: 20 additions & 0 deletions app/Events/Common/ItemDeleting.php
@@ -0,0 +1,20 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;

class ItemDeleting extends Event
{
public $item;

/**
* Create a new event instance.
*
* @param $item
*/
public function __construct($item)
{
$this->item = $item;
}
}
25 changes: 25 additions & 0 deletions app/Events/Common/ItemUpdated.php
@@ -0,0 +1,25 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;
use App\Models\Common\Item;

class ItemUpdated extends Event
{
public $item;

public $request;

/**
* Create a new event instance.
*
* @param $item
* @param $request
*/
public function __construct(Item $item, $request)
{
$this->item = $item;
$this->request = $request;
}
}
25 changes: 25 additions & 0 deletions app/Events/Common/ItemUpdating.php
@@ -0,0 +1,25 @@
<?php

namespace App\Events\Common;

use App\Abstracts\Event;
use App\Models\Common\Item;

class ItemUpdating extends Event
{
public $item;

public $request;

/**
* Create a new event instance.
*
* @param $item
* @param $request
*/
public function __construct(Item $item, $request)
{
$this->item = $item;
$this->request = $request;
}
}
6 changes: 6 additions & 0 deletions app/Jobs/Common/CreateItem.php
Expand Up @@ -3,6 +3,8 @@
namespace App\Jobs\Common;

use App\Abstracts\Job;
use App\Events\Common\ItemCreated;
use App\Events\Common\ItemCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
Expand All @@ -13,6 +15,8 @@ class CreateItem extends Job implements HasOwner, HasSource, ShouldCreate
{
public function handle(): Item
{
event(new ItemCreating($this->request));

\DB::transaction(function () {
$this->model = Item::create($this->request->all());

Expand All @@ -26,6 +30,8 @@ public function handle(): Item
$this->dispatch(new CreateItemTaxes($this->model, $this->request));
});

event(new ItemCreated($this->model, $this->request));

return $this->model;
}
}
6 changes: 6 additions & 0 deletions app/Jobs/Common/DeleteItem.php
Expand Up @@ -3,6 +3,8 @@
namespace App\Jobs\Common;

use App\Abstracts\Job;
use App\Events\Common\ItemDeleted;
use App\Events\Common\ItemDeleting;
use App\Interfaces\Job\ShouldDelete;

class DeleteItem extends Job implements ShouldDelete
Expand All @@ -11,12 +13,16 @@ public function handle(): bool
{
$this->authorize();

event(new ItemDeleting($this->model));

\DB::transaction(function () {
$this->deleteRelationships($this->model, ['taxes']);

$this->model->delete();
});

event(new ItemDeleted($this->model));

return true;
}

Expand Down
6 changes: 6 additions & 0 deletions app/Jobs/Common/UpdateItem.php
Expand Up @@ -3,6 +3,8 @@
namespace App\Jobs\Common;

use App\Abstracts\Job;
use App\Events\Common\ItemUpdated;
use App\Events\Common\ItemUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Jobs\Common\CreateItemTaxes;
use App\Models\Common\Item;
Expand All @@ -11,6 +13,8 @@ class UpdateItem extends Job implements ShouldUpdate
{
public function handle(): Item
{
event(new ItemUpdating($this->model, $this->request));

\DB::transaction(function () {
$this->model->update($this->request->all());

Expand All @@ -26,6 +30,8 @@ public function handle(): Item
$this->dispatch(new CreateItemTaxes($this->model, $this->request));
});

event(new ItemUpdated($this->model, $this->request));

return $this->model;
}
}

0 comments on commit 6af4c75

Please sign in to comment.