Skip to content

Instantly share code, notes, and snippets.

@geomagilles
Last active March 26, 2018 10:33
Show Gist options
  • Save geomagilles/7a372915ff81862f00be20c9bdd2315b to your computer and use it in GitHub Desktop.
Save geomagilles/7a372915ff81862f00be20c9bdd2315b to your computer and use it in GitHub Desktop.
<?php
use Zenaton\Interfaces\WorkflowInterface;
use Zenaton\Traits\Zenatonable;
use Zenaton\Tasks\Wait;
class NotifyEtaWorkflow implements WorkflowInterface
{
use Zenatonable;
// inform user # seconds before ETA
const BEFORE = 3600;
// precision target
const PRECISION = 120;
// update threshold : inform user if ETA changed more than # seconds
const UPDATE = 1200;
// trip id
protected $tripId;
// user to notify
protected $user;
public function __construct($tripId, $user)
{
$this->tripId = $tripId;
$this->user = $user;
}
public function handle()
{
// calulate current ETA
[$duration, $eta] = $this->getTimeToArrival();
// retry calculation until $duration is close enough to 1h
while ($duration > self::BEFORE + self::PRECISION) {
// wait half of lasting time before notification
(new Wait())->seconds(($duration - self::BEFORE) / 2)->execute();
// recalculate duration and eta
[$duration, $eta] = $this->getTimeToArrival();
}
// send message
(new InformUserOfEtaTask($this->user, $eta))->execute();
// inform user of significant change of ETA until arrival
while ($duration > self::PRECISION) {
// wait 20 min
(new Wait())->seconds(self::UPDATE)->execute();
// recalculate duration and eta
[$duration, $eta2] = $this->getTimeToArrival();
// if new eta changed significantly, send new notification
if (abs($eta2 - $eta) >= self::UPDATE) {
// inform user of updated ETA
$eta = $eta2;
(new NotifyUserOfUpdatedEtaTask($this->user, $eta))->execute();
}
}
}
protected function getTimeToArrival()
{
return (new CalculateTimeToArrivalTask($this->tripId))->execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment