| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Events\Order; |
| 4 |
|
| 5 |
use FluentCart\App\Events\EventDispatcher; |
| 6 |
use FluentCart\App\Listeners; |
| 7 |
use FluentCart\App\Models\Order; |
| 8 |
use FluentCart\Framework\Support\Arr; |
| 9 |
|
| 10 |
class OrderStatusUpdated extends EventDispatcher |
| 11 |
{ |
| 12 |
public string $hook = 'fluent_cart/order_status_updated'; |
| 13 |
public bool $autoFireHook = false; |
| 14 |
|
| 15 |
protected array $listeners = [ |
| 16 |
// Listeners\UpdateStock::class, |
| 17 |
]; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Order $order |
| 21 |
*/ |
| 22 |
public Order $order; |
| 23 |
public ?string $oldStatus; |
| 24 |
public ?string $newStatus; |
| 25 |
public ?bool $manageStock; |
| 26 |
public array $activity = []; |
| 27 |
|
| 28 |
public string $type; |
| 29 |
|
| 30 |
protected $willDispatch = true; |
| 31 |
|
| 32 |
public function __construct(Order $order, $oldStatus = null, $newStatus = null, $manageStock = true, $activity = [], $type = 'payment_status') |
| 33 |
{ |
| 34 |
$this->order = $order; |
| 35 |
|
| 36 |
if ($oldStatus === $newStatus) { |
| 37 |
$this->willDispatch = false; |
| 38 |
$this->listeners = []; |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$this->order->load('customer', 'shipping_address', 'billing_address'); |
| 43 |
$this->oldStatus = $oldStatus; |
| 44 |
$this->newStatus = $newStatus; |
| 45 |
$this->manageStock = $manageStock; |
| 46 |
$this->activity = $activity; |
| 47 |
$this->type = $type; |
| 48 |
} |
| 49 |
|
| 50 |
public function toArray(): array |
| 51 |
{ |
| 52 |
return [ |
| 53 |
'order' => $this->order, |
| 54 |
'old_status' => $this->oldStatus, |
| 55 |
'new_status' => $this->newStatus, |
| 56 |
'manageStock' => $this->manageStock, |
| 57 |
'activity' => $this->activity, |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function getActivityEventModel() |
| 62 |
{ |
| 63 |
return $this->order; |
| 64 |
} |
| 65 |
|
| 66 |
public function getEventInfoForActivity(): array |
| 67 |
{ |
| 68 |
return [ |
| 69 |
'title' => Arr::get($this->activity, 'title', ''), |
| 70 |
'content' => Arr::get($this->activity, 'content', '') |
| 71 |
]; |
| 72 |
} |
| 73 |
|
| 74 |
public function afterDispatch() |
| 75 |
{ |
| 76 |
if (!$this->willDispatch) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if ($this->type === 'payment_status') { |
| 81 |
do_action('fluent_cart/payment_status_changed_to_'.$this->newStatus, $this->toArray()); |
| 82 |
do_action('fluent_cart/payment_status_changed', $this->toArray()); |
| 83 |
} |
| 84 |
|
| 85 |
if ($this->type === 'shipping_status') { |
| 86 |
do_action('fluent_cart/shipping_status_changed_to_' . $this->newStatus, $this->toArray()); |
| 87 |
do_action('fluent_cart/shipping_status_changed', $this->toArray()); |
| 88 |
} |
| 89 |
|
| 90 |
if ($this->type === 'order_status') { |
| 91 |
do_action('fluent_cart/order_status_changed_to_' . $this->newStatus, $this->toArray()); |
| 92 |
do_action('fluent_cart/order_status_changed', $this->toArray()); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|