Skip to content

Instantly share code, notes, and snippets.

@hvent90
Created January 6, 2015 01:25
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 hvent90/c931cc0a14f7bcf64233 to your computer and use it in GitHub Desktop.
Save hvent90/c931cc0a14f7bcf64233 to your computer and use it in GitHub Desktop.
<?php namespace Carma\Events;
use Caster\Models\Incident;
use Caster\Models\IncidentType;
use Caster\Models\IncidentDetail;
class IncidentEventHandler {
/**
* Let's create the incident!
* @param Array $values [potentially unformatted values]
* @return Caster\Models\Incident $incident
*/
public function onCreate(Array $values)
{
$incident = Incident::create($values);
/*
* Grab the IncidentType based off of the Type submitted
* and create the relationship between Incident and IncidentType.
*/
try
{
$incidentType = IncidentType::where('name', $incident->type)->findOrFail();
$incident->type_id = $incidentType->id;
}
catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e)
{
return $this->errorNotFound(Lang::get('incident-type.not_found'));
}
/*
* Create a new IncidentDetail and create a relationship
* between the IncidentDetail and Incident if the user supplied
* text for the Incident's detail.
*/
if($incident->details) {
$incidentDetail = new IncidentDetail;
$incidentDetail->text = $incident->details;
$incidentDetail->save;
$incident->details = $incidentDetail->id;
}
/*
* Determine the Severity based on Type
*/
switch ($incident->type) {
case 'Traffic Jam':
switch ($incident->severity) {
case 'Heavy':
$incident->severity = 4;
case 'Moderate':
$incident->severity = 2;
case 'Light':
$incident->severity = 1;
}
case 'Police':
$incident->severity = 3;
case 'Accident':
switch ($incident->severity) {
case 'Major':
$incident->severity = 4;
case 'Minor':
$incident->severity = 2;
}
case 'Road Close':
$incident->severity = 4;
}
/*
* Reverse the direction if 'other_side' is true.
*/
if($incident->other_side) {
switch($incident->direction) {
case 'WB':
$incident->direction = 'EB';
case 'EB':
$incident->direction = 'WB';
case 'NB':
$incident->direction = 'SB';
case 'SB':
$incident->direction = 'NB';
}
}
/*
* Save the Incident!
*/
$incident->save();
return $incident;
}
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
* @return array
*/
public function subscribe($events)
{
$events->listen('incident.create', 'Carma\Events\IncidentEventHandler@onCreate');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment