master

laravel/framework

Last updated at: 29/12/2023 09:23

BroadcastNotificationCreated.php

TLDR

This file, BroadcastNotificationCreated.php, is part of the Illuminate\Notifications\Events namespace and contains the BroadcastNotificationCreated class. This class implements the ShouldBroadcast interface and includes several public and protected properties and methods related to broadcasting notifications.

Methods

broadcastOn()

This method determines the channels on which the notification event should be broadcast. It checks if the notifiable entity is an instance of AnonymousNotifiable and has a route to broadcast notifications. If the notifiable entity does not have a route, it calls the broadcastOn() method of the notification instance. The method then returns an array of channels or an instance of PrivateChannel.

channelName()

This protected method retrieves the broadcast channel name for the event. It checks if the notifiable entity has a method receivesBroadcastNotificationsOn() and calls it, passing the notification instance as an argument. If no such method exists, it generates a channel name based on the class name of the notifiable entity and its key.

broadcastWith()

This method retrieves the data to be sent with the broadcasted event. It checks if the notification instance has a broadcastWith() method and calls it to retrieve additional data. It then combines this additional data with the data property of the event and returns the merged array.

broadcastType()

This method retrieves the type of the notification being broadcasted. It checks if the notification instance has a broadcastType() method and calls it. If the method does not exist, it returns the class name of the notification instance.

broadcastAs()

This method retrieves the event name of the notification being broadcasted. It checks if the notification instance has a broadcastAs() method and calls it. If the method does not exist, it returns the fully qualified class name of the BroadcastNotificationCreated class.

Classes

There are no additional classes defined in this file.

<?php

namespace Illuminate\Notifications\Events;

use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;

class BroadcastNotificationCreated implements ShouldBroadcast
{
    use Queueable, SerializesModels;

    /**
     * The notifiable entity who received the notification.
     *
     * @var mixed
     */
    public $notifiable;

    /**
     * The notification instance.
     *
     * @var \Illuminate\Notifications\Notification
     */
    public $notification;

    /**
     * The notification data.
     *
     * @var array
     */
    public $data = [];

    /**
     * Create a new event instance.
     *
     * @param  mixed  $notifiable
     * @param  \Illuminate\Notifications\Notification  $notification
     * @param  array  $data
     * @return void
     */
    public function __construct($notifiable, $notification, $data)
    {
        $this->data = $data;
        $this->notifiable = $notifiable;
        $this->notification = $notification;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        if ($this->notifiable instanceof AnonymousNotifiable &&
            $this->notifiable->routeNotificationFor('broadcast')) {
            $channels = Arr::wrap($this->notifiable->routeNotificationFor('broadcast'));
        } else {
            $channels = $this->notification->broadcastOn();
        }

        if (! empty($channels)) {
            return $channels;
        }

        if (is_string($channels = $this->channelName())) {
            return [new PrivateChannel($channels)];
        }

        return collect($channels)->map(function ($channel) {
            return new PrivateChannel($channel);
        })->all();
    }

    /**
     * Get the broadcast channel name for the event.
     *
     * @return array|string
     */
    protected function channelName()
    {
        if (method_exists($this->notifiable, 'receivesBroadcastNotificationsOn')) {
            return $this->notifiable->receivesBroadcastNotificationsOn($this->notification);
        }

        $class = str_replace('\\', '.', get_class($this->notifiable));

        return $class.'.'.$this->notifiable->getKey();
    }

    /**
     * Get the data that should be sent with the broadcasted event.
     *
     * @return array
     */
    public function broadcastWith()
    {
        if (method_exists($this->notification, 'broadcastWith')) {
            return $this->notification->broadcastWith();
        }

        return array_merge($this->data, [
            'id' => $this->notification->id,
            'type' => $this->broadcastType(),
        ]);
    }

    /**
     * Get the type of the notification being broadcast.
     *
     * @return string
     */
    public function broadcastType()
    {
        return method_exists($this->notification, 'broadcastType')
                    ? $this->notification->broadcastType()
                    : get_class($this->notification);
    }

    /**
     * Get the event name of the notification being broadcast.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return method_exists($this->notification, 'broadcastAs')
                ? $this->notification->broadcastAs()
                : __CLASS__;
    }
}