UniqueBroadcastEvent.php
TLDR
This file, UniqueBroadcastEvent.php
, is a class in the Illuminate\Broadcasting
namespace that extends the BroadcastEvent
class and implements the ShouldBeUnique
interface. It provides functionality for creating unique broadcast events that are managed by a cache implementation. The UniqueBroadcastEvent
class has a constructor and a uniqueVia
method.
__construct
The __construct
method initializes the uniqueId
and uniqueFor
properties of the UniqueBroadcastEvent
object. It accepts an $event
parameter and sets the uniqueId
based on the class name of the $event
. If the $event
object has a uniqueId
method or a uniqueId
property, it appends the value to the uniqueId
property of UniqueBroadcastEvent
. It does the same for the uniqueFor
property. Finally, it passes the $event
parameter to the parent BroadcastEvent
constructor.
uniqueVia
The uniqueVia
method resolves the cache implementation that should manage the event's uniqueness. If the $event
object has a uniqueVia
method, it calls that method and returns its result. Otherwise, it uses the Container::getInstance()->make(Repository::class)
method to create an instance of the Repository
class and returns it.
<?php
namespace Illuminate\Broadcasting;
use Illuminate\Container\Container;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Queue\ShouldBeUnique;
class UniqueBroadcastEvent extends BroadcastEvent implements ShouldBeUnique
{
/**
* The unique lock identifier.
*
* @var mixed
*/
public $uniqueId;
/**
* The number of seconds the unique lock should be maintained.
*
* @var int
*/
public $uniqueFor;
/**
* Create a new event instance.
*
* @param mixed $event
* @return void
*/
public function __construct($event)
{
$this->uniqueId = get_class($event);
if (method_exists($event, 'uniqueId')) {
$this->uniqueId .= $event->uniqueId();
} elseif (property_exists($event, 'uniqueId')) {
$this->uniqueId .= $event->uniqueId;
}
if (method_exists($event, 'uniqueFor')) {
$this->uniqueFor = $event->uniqueFor();
} elseif (property_exists($event, 'uniqueFor')) {
$this->uniqueFor = $event->uniqueFor;
}
parent::__construct($event);
}
/**
* Resolve the cache implementation that should manage the event's uniqueness.
*
* @return \Illuminate\Contracts\Cache\Repository
*/
public function uniqueVia()
{
return method_exists($this->event, 'uniqueVia')
? $this->event->uniqueVia()
: Container::getInstance()->make(Repository::class);
}
}