ToastEvent::getEventJSCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
The trait MallardDuck\DynamicEcho\...seDynamicChannelFormula requires some properties which are not provided by App\Events\ToastEvent: $channelAuthName, $eventChannelIdentifierBindingCallback, $channelType
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
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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