Skip to content

Instantly share code, notes, and snippets.

@ryanmr
Last active January 30, 2016 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmr/85c97b8480e966e3274f to your computer and use it in GitHub Desktop.
Save ryanmr/85c97b8480e966e3274f to your computer and use it in GitHub Desktop.
<?php
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
// use Log;
use App\Episode;
use App\Events\EpisodeWasCreated;
class ApplyEpisodeDefaults
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param EpisodeWasCreated $event
* @return void
*/
public function handle(EpisodeWasCreated $event)
{
$episode = $event->getEpisode();
// \Log::debug('episode created: EpisodeWasCreated event fired', ['episode' => $episode]);
$this->saveArt($episode);
$this->saveHosts($episode);
}
private function saveArt(Episode $episode) {
$art = $episode->art;
// detect if the episode has any art attach
// if not, attach series art if available
if ($episode->art == null) {
$series_art = $episode->series->art;
if ($series_art->count() > 0) {
$episode->album_art_id = $series_art->first()->id;
$episode->save();
}
}
}
private function saveHosts(Episode $episode) {
$people = $episode->people;
if (count($people) == 0) {
$series_hosts = $episode->series->hosts;
if ($series_hosts->count() > 0) {
$ids = $series_hosts->map(function($i, $k){
return $i->id;
})->all();
// Log::debug('series hosts', ['sh' => $series_hosts->all(), 'ids' => $ids]);
$data = [];
foreach ($ids as $key => $id) {
$data[$id] = ['role' => 'host'];
}
$r = $episode->people()->sync($data);
// Log::debug('result of it', ['r' => $r]);
$episode->save();
}
}
}
}
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Events\EpisodeWasCreated;
class Episode extends Model {
protected $hidden = [];
/**
* Add the specified custom attributes to the toArray/toJson versions of this model.
* @var array
*/
protected $appends = ['formal_name'];
/* Relationships */
public function related() {
return $this->belongsToMany('App\Episode', 'episodes_relations', 'episode_id', 'episode_related_id')->withPivot('type');
}
public function series() {
return $this->belongsTo('App\Series', 'series_id');
}
public function media() {
return $this->hasMany('App\EpisodeMedia', 'episode_id');
}
public function people() {
return $this->belongsToMany('App\Person', 'people_episodes', 'episode_id', 'person_id')->withPivot('role');
}
public function art() {
return $this->belongsTo('App\AlbumArt', 'album_art_id');
}
public function getDefaultArt() {
return $this->series->art;
}
/* Scopes */
public function scopeVisible($query) {
return $query
->where('hidden', '=', false)
->where('unlisted', '=', false)
->where('state', '=', 'published');
}
public function scopeAccessible($query) {
return $query
->where('hidden', '=', false)
->where(function($query){
$query
->where('state', '=', 'preview')
->orWhere('state', '=', 'published');
});
}
public function scopeRecent($query) {
return $query->orderBy('created_at', 'DESC');
}
public function scopeWithSlug($query, $slug) {
return $query->whereHas('series', function($q) use ($slug) {$q->where('slug', '=', $slug);});
}
public function getFormalName() {
$name = $this->title;
$number = $this->number;
$series = $this->series->name;
if ( $this->isFringe() && $this->hasParent() ) {
$id = $this->id;
$parent = $this->getParent();
$slug = strtoupper($parent->series->slug);
$pnumber = $parent->number;
$name = "$slug $pnumber — $name";
}
$template = "$series #$number: $name";
return $template;
}
public function getFormalNameAttribute() {
return $this->getFormalName();
}
private function _getRelated($target) {
$related = $this->related;
$related = $related->filter(function($item) use ($target) {
if ( $item->pivot->type == $target ) {
return true;
}
return false;
});
return $related->first();
}
public function getParent() {
return $this->_getRelated('parent');
}
public function getFringe() {
return $this->_getRelated('fringe');
}
public function isNew() {
$created_at = $this->created_at;
$against = strtotime("-7 days");
return strtotime($created_at) > $against;
}
public function isFringe() {
$series = $this->series->slug;
return $series == 'tf';
}
public function hasFringe() {
return $this->getFringe() != null;
}
public function hasParent() {
return $this->getParent() != null;
}
// public function save(array $options = []) {
// parent::save($options);
//
// \Event::fire(new EpisodeWasSaved($this));
// }
}
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Episode;
class EpisodeWasCreated extends Event
{
use SerializesModels;
private $episode;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Episode $episode)
{
$this->episode = $episode;
}
public function getEpisode() {
return $this->episode;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
<?php namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider {
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\EpisodeWasCreated' => [
'App\Listeners\ApplyEpisodeDefaults',
],
];
/**
* Register any other events for your application.
*
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
\App\Episode::created(function(\App\Episode $episode){
event(new \App\Events\EpisodeWasCreated($episode));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment