Skip to content

Instantly share code, notes, and snippets.

@geomagilles
Last active March 26, 2018 10:27
Show Gist options
  • Save geomagilles/ed007aa6276975f799cbe5e0ff84c7cb to your computer and use it in GitHub Desktop.
Save geomagilles/ed007aa6276975f799cbe5e0ff84c7cb 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;
// 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();
}
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