Skip to content

Commit

Permalink
broadcast events using new style jobs (#17433)
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell authored Jan 19, 2017
1 parent 89d50c4 commit 33917ed
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
38 changes: 18 additions & 20 deletions src/Illuminate/Broadcasting/BroadcastEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,49 @@

use ReflectionClass;
use ReflectionProperty;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Broadcasting\Broadcaster;

class BroadcastEvent
class BroadcastEvent implements ShouldQueue
{
use Queueable;

/**
* The broadcaster implementation.
* The event instance.
*
* @var \Illuminate\Contracts\Broadcasting\Broadcaster
* @var mixed
*/
protected $broadcaster;
protected $event;

/**
* Create a new job handler instance.
*
* @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster
* @param mixed $event
* @return void
*/
public function __construct(Broadcaster $broadcaster)
public function __construct($event)
{
$this->broadcaster = $broadcaster;
$this->event = $event;
}

/**
* Handle the queued job.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @param array $data
* @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster
* @return void
*/
public function fire(Job $job, array $data)
public function handle(Broadcaster $broadcaster)
{
$event = unserialize($data['event']);
$name = method_exists($this->event, 'broadcastAs')
? $this->event->broadcastAs() : get_class($this->event);

$name = method_exists($event, 'broadcastAs')
? $event->broadcastAs() : get_class($event);

$channels = array_wrap($event->broadcastOn());

$this->broadcaster->broadcast(
$channels, $name, $this->getPayloadFromEvent($event)
$broadcaster->broadcast(
array_wrap($this->event->broadcastOn()), $name,
$this->getPayloadFromEvent($this->event)
);

$job->delete();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Broadcasting/BroadcastManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function queue($event)
}

$this->app->make('queue')->connection($connection)->pushOn(
$queue, BroadcastEvent::class, ['event' => serialize(clone $event)]
$queue, new BroadcastEvent(clone $event)
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Broadcasting/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
],
"require": {
"php": ">=5.6.4",
"illuminate/bus": "5.4.*",
"illuminate/contracts": "5.4.*",
"illuminate/queue": "5.4.*",
"illuminate/support": "5.4.*"
},
"autoload": {
Expand Down
14 changes: 2 additions & 12 deletions tests/Broadcasting/BroadcastEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ public function testBasicEventBroadcastParameterFormatting()
);

$event = new TestBroadcastEvent;
$serializedEvent = serialize($event);
$jobData = ['event' => $serializedEvent];

$job = m::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('delete')->once();

(new \Illuminate\Broadcasting\BroadcastEvent($broadcaster))->fire($job, $jobData);
(new \Illuminate\Broadcasting\BroadcastEvent($event))->handle($broadcaster);
}

public function testManualParameterSpecification()
Expand All @@ -39,13 +34,8 @@ public function testManualParameterSpecification()
);

$event = new TestBroadcastEventWithManualData;
$serializedEvent = serialize($event);
$jobData = ['event' => $serializedEvent];

$job = m::mock('Illuminate\Contracts\Queue\Job');
$job->shouldReceive('delete')->once();

(new \Illuminate\Broadcasting\BroadcastEvent($broadcaster))->fire($job, $jobData);
(new \Illuminate\Broadcasting\BroadcastEvent($event))->handle($broadcaster);
}
}

Expand Down

0 comments on commit 33917ed

Please sign in to comment.