Last active
July 6, 2017 14:27
-
-
Save sirawitpra/bbe8ee8b0aa374c7d8289221723784d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Listeners; | |
use App\Events\OrderCreated; | |
use App\Events\StockIsLow; | |
use App\Product; | |
class UpdateStock | |
{ | |
/** | |
* Create the event listener. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
// | |
} | |
/** | |
* Handle the event. | |
* | |
* @param OrderCreated $event | |
* @return void | |
*/ | |
public function handle(OrderCreated $event) | |
{ | |
foreach ($event->order->items as $item) { | |
$product = Product::find($item->productId); | |
$quantityLeft = $product->quantity - $item->quantity; | |
$product->update([ | |
'quantity' => $quantityLeft | |
]); | |
if ($quantityLeft < 3) { | |
event(new StockIsLow($product)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment