Skip to content

Instantly share code, notes, and snippets.

@qpleple
Created December 16, 2016 15:47
Show Gist options
  • Save qpleple/50c9c0d221c5f1e1c8b3fc296cb04c88 to your computer and use it in GitHub Desktop.
Save qpleple/50c9c0d221c5f1e1c8b3fc296cb04c88 to your computer and use it in GitHub Desktop.
<?php
class BaseEvent
{
protected $eventName = '';
public function occured()
{
$this->dispatcher->dispatch('event.' . $eventName . '.received');
}
}
class BasePeriod
{
protected $periodName = '';
public function start()
{
$this->dispatcher->dispatch('period.' . $periodName . '.start');
}
public function end()
{
$this->dispatcher->dispatch('period.' . $periodName . '.end');
}
public function closeIfOpen()
{
if (false) {
return;
}
// close
$this->end();
}
}
/** @Service */
class ConnectedWatcher extends BasePeriod
{
protected $activity;
protected $periodName = 'CONNECTED';
function __construct($activity)
{
$this->activity = $activity;
}
/** @Observe("event.packet_received") */
public function onPacketReceived()
{
$this->openIfNotAlreadyOpen();
$this->resetTimeOut('no_packet_received', 20 * 60);
}
public function timedOut()
{
$this->closeIfOpen();
}
}
class StatsCall extends BaseEvent
{
// parse la data de /stats et créé un objet Event
$event = new StatsEvent($data);
$this->dispatch('event.stats_call.occured', $event);
}
class ButtonCall extends BaseEvent
{
$event = new ButtonEvent($data);
$this->dispatch('event.button_call.occured', $event);
}
class StatusCall extends BaseEvent
{
$event = new StatusEvent($data);
$this->dispatcher->dispatch('event.status_call.occured', $event);
}
/** @Service */
class ApplicationStatus extends BaseEvent
{
protected $activity;
protected $eventName = 'application_status';
/** @Observe("event.button_call.occured") */
public function onButtonCall($event)
{
$this->onNewStatus($event->getStatus());
}
/** @Observe("event.status_call.occured") */
public function onStatusCall($event)
{
$this->onNewStatus($event->getStatus());
}
/** @Observe("event.stats_call.occured") */
public function onStatusCall($event)
{
$this->onNewStatus($event->getStatus());
}
public function onNewStatus($status)
{
$this->persistNewStatus($status);
$this->dispatcher->dispatch('event.status.occured', $status);
}
}
/** @Service */
class Jam extends BasePeriod
{
/** @Observe("event.status.occured") */
public function onStatus($status)
{
if ($status != 'PAPER_JAM') {
return;
}
$created = $this->createIfNotExist();
if ($created) {
$this->dispatcher->dispatch('period.jam.start');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment