| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Events; |
| 4 |
|
| 5 |
use FluentCart\App\Listeners\Activity; |
| 6 |
use FluentCart\Framework\Support\ArrayableInterface; |
| 7 |
|
| 8 |
abstract class EventDispatcher implements ArrayableInterface |
| 9 |
{ |
| 10 |
protected array $listeners = []; |
| 11 |
public string $hook = ''; |
| 12 |
public bool $autoFireHook = true; |
| 13 |
|
| 14 |
|
| 15 |
public function dispatch() |
| 16 |
{ |
| 17 |
$this->beforeDispatch(); |
| 18 |
$iterableListeners = $this->listeners; |
| 19 |
$iterableListeners[] = Activity::class; |
| 20 |
|
| 21 |
foreach ($iterableListeners as $listener) { |
| 22 |
$listener::handle($this); |
| 23 |
} |
| 24 |
|
| 25 |
$this->afterDispatch(); |
| 26 |
|
| 27 |
if (!empty($this->hook) && $this->autoFireHook) { |
| 28 |
do_action($this->hook, $this->toArray()); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
public function shouldCreateActivity(): bool |
| 33 |
{ |
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
public function getEventInfoForActivity(): array |
| 38 |
{ |
| 39 |
return []; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* @return mixed |
| 45 |
*/ |
| 46 |
abstract public function getActivityEventModel(); |
| 47 |
|
| 48 |
public function beforeDispatch() |
| 49 |
{ |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
public function afterDispatch() |
| 54 |
{ |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|