shetabit /
multipay
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Shetabit\Multipay\Traits; |
||
| 4 | |||
| 5 | use Shetabit\Multipay\EventEmitter; |
||
| 6 | |||
| 7 | trait HasPaymentEvents |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Event registerar. |
||
| 11 | * |
||
| 12 | * @var EventEmitter |
||
| 13 | */ |
||
| 14 | protected static $eventEmitter; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Add verification event listener. |
||
| 18 | * |
||
| 19 | * @param callable $listener |
||
| 20 | * |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | public static function addPurchaseListener(callable $listener) |
||
| 24 | { |
||
| 25 | static::singletoneEventEmitter(); |
||
| 26 | |||
| 27 | static::$eventEmitter->addEventListener('purchase', $listener); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Remove verification event listener. |
||
| 32 | * |
||
| 33 | * @param callable|null $listener |
||
| 34 | * |
||
| 35 | * @return void |
||
| 36 | */ |
||
| 37 | public static function removePurchaseListener(callable $listener = null) |
||
| 38 | { |
||
| 39 | static::singletoneEventEmitter(); |
||
| 40 | |||
| 41 | static::$eventEmitter->removeEventListener('purchase', $listener); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Add pay event listener. |
||
| 46 | * |
||
| 47 | * @param callable $listener |
||
| 48 | * |
||
| 49 | * @return void |
||
| 50 | */ |
||
| 51 | public static function addPayListener(callable $listener) |
||
| 52 | { |
||
| 53 | static::singletoneEventEmitter(); |
||
| 54 | |||
| 55 | static::$eventEmitter->addEventListener('pay', $listener); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Remove pay event listener. |
||
| 60 | * |
||
| 61 | * @param callable|null $listener |
||
| 62 | * |
||
| 63 | * @return void |
||
| 64 | */ |
||
| 65 | public static function removePayListener(callable $listener = null) |
||
| 66 | { |
||
| 67 | static::singletoneEventEmitter(); |
||
| 68 | |||
| 69 | static::$eventEmitter->removeEventListener('pay', $listener); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Add verification event listener. |
||
| 74 | * |
||
| 75 | * @param callable $listener |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public static function addVerifyListener(callable $listener) |
||
| 80 | { |
||
| 81 | static::singletoneEventEmitter(); |
||
| 82 | |||
| 83 | static::$eventEmitter->addEventListener('verify', $listener); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Remove verification event listener. |
||
| 88 | * |
||
| 89 | * @param callable|null $listener |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | public static function removeVerifyListener(callable $listener = null) |
||
| 94 | { |
||
| 95 | static::singletoneEventEmitter(); |
||
| 96 | |||
| 97 | static::$eventEmitter->removeEventListener('verify', $listener); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Dispatch an event. |
||
| 102 | * |
||
| 103 | * @param string $event |
||
| 104 | * @param ...$arguments |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | protected function dispatchEvent(string $event, ...$arguments) |
||
| 109 | { |
||
| 110 | static::singletoneEventEmitter(); |
||
| 111 | |||
| 112 | static::$eventEmitter->dispatch($event, ...$arguments); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Add an singletone event registerar. |
||
| 117 | * |
||
| 118 | * @return void |
||
| 119 | */ |
||
| 120 | protected static function singletoneEventEmitter() |
||
| 121 | { |
||
| 122 | if (static::$eventEmitter instanceof EventEmitter) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 123 | return; |
||
| 124 | } |
||
| 125 | |||
| 126 | static::$eventEmitter = new EventEmitter; |
||
| 127 | } |
||
| 128 | } |
||
| 129 |