mallardduck /
dynamic-echo-events
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Events; |
||
| 4 | |||
| 5 | use Illuminate\Broadcasting\InteractsWithSockets; |
||
| 6 | use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow; |
||
| 7 | use Illuminate\Foundation\Events\Dispatchable; |
||
| 8 | use Illuminate\Queue\SerializesModels; |
||
| 9 | use Illuminate\Support\Facades\Auth; |
||
| 10 | use MallardDuck\DynamicEcho\Channels\{ |
||
| 11 | BaseDynamicChannelFormula, |
||
| 12 | PrivateUserChannelParameters, |
||
| 13 | }; |
||
| 14 | use MallardDuck\DynamicEcho\Contracts\{ |
||
| 15 | HasDynamicChannelFormula, |
||
| 16 | ImplementsDynamicEcho, |
||
| 17 | }; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Class ToastEvent |
||
| 21 | * |
||
| 22 | * @mixin HasDynamicChannelFormula |
||
| 23 | * @mixin BaseDynamicChannelFormula |
||
| 24 | */ |
||
| 25 | class ToastEvent implements ShouldBroadcastNow, ImplementsDynamicEcho, HasDynamicChannelFormula |
||
| 26 | { |
||
| 27 | use Dispatchable, InteractsWithSockets, BaseDynamicChannelFormula; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 28 | |||
| 29 | public int $userId; |
||
| 30 | |||
| 31 | public string $type; |
||
| 32 | |||
| 33 | public string $message; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Create a new event instance. |
||
| 37 | * |
||
| 38 | * @param string $type |
||
| 39 | * @param string $message |
||
| 40 | * @param null|int $userId |
||
| 41 | */ |
||
| 42 | public function __construct(string $type, string $message, ?int $userId = null) |
||
| 43 | { |
||
| 44 | $this->type = $type; |
||
| 45 | $this->message = $message; |
||
| 46 | $this->userId = $userId ?? Auth::user()->id; |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | |||
| 49 | public static function getChannelParametersClassname(): string |
||
| 50 | { |
||
| 51 | return PrivateUserChannelParameters::class; |
||
| 52 | } |
||
| 53 | |||
| 54 | public static function getEventJSCallback(): string |
||
| 55 | { |
||
| 56 | return <<<JSCALLBACK |
||
| 57 | (e) => { |
||
| 58 | console.log(`User: \${e.userId} received a "\${e.type}" type toast event.`); |
||
| 59 | const type = e.type; |
||
| 60 | const message = e.message; |
||
| 61 | window.toastr[type](message); |
||
| 62 | } |
||
| 63 | JSCALLBACK; |
||
| 64 | } |
||
| 65 | } |
||
| 66 |