Laravel: The Magic of Events, Listeners and Jobs

On a side note, Steve Jobs was also magical

Only Jobs can be scheduled

XKCD #583
$podcast = Podcast::find(2);$event = new PodcastUploaded($podcast);$this->schedule($event)->weekly(); // Now rap with me:
// F*ck the police comin' straight from the underground...

Listeners vs Jobs handling of Services

<?php

namespace App\Listeners;

use App\Events\PodcastUploaded;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Bus\Queueable;
class UploadListener implements ShouldQueue
{
use InteractsWithQueue,
Queueable;
/**
* Podcast Transcoder
*
* @var \AudioMagic\AudioTranscoder
*/
protected $transcoder;
/**
* Create the event listener.
*
* @param \AudioMagic\AudioTranscoder $transcoder
* @return void
*/
public function __construct(AudioTranscoder $transcoder)
{
$this->transcoder = $transcoder;
}

/**
* Handle the event.
*
* @param \App\Events\PodcastUploaded $event
* @return void
*/
public function handle(PodcastUploaded $event)
{
$transcoded = $this->transcoder->process()
->format('mp3')
->bitrate('128')
->file($event->podcast->path)
->output($event->podcast->path . '_128.mp3')
->start();
if ($transcoded->success()) {
// Do something with the transcoded file
}
// ... }
}

Chaining Everything

Professional artistic representation of the chain
Butterfly Effect. A single Podcast uploaded can run multiple logics in the application.

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.