From 08e9705088d36543c8cc0eccfa5dc263d69b994a Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sat, 26 Sep 2015 14:42:42 +0100 Subject: [PATCH] $incident_date is now handled by the command handler --- .../Incident/ReportIncidentCommand.php | 26 +++++++++++++------ .../Incident/UpdateIncidentCommand.php | 9 ++++++- .../Incident/ReportIncidentCommandHandler.php | 13 ++++++++++ .../Incident/UpdateIncidentCommandHandler.php | 8 +++++- .../Controllers/Api/IncidentController.php | 6 +++-- .../Dashboard/IncidentController.php | 25 +++--------------- 6 files changed, 53 insertions(+), 34 deletions(-) diff --git a/app/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php index 25486c75..988eb653 100644 --- a/app/Commands/Incident/ReportIncidentCommand.php +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -62,6 +62,13 @@ class ReportIncidentCommand */ public $notify; + /** + * The date at which the incident occurred. + * + * @var string|null + */ + public $incident_date; + /** * The validation rules. * @@ -75,22 +82,24 @@ class ReportIncidentCommand 'component_id' => 'integer', 'component_status' => 'integer', 'notify' => 'boolean', + 'incident_date' => 'string', ]; /** * Create a new report incident command instance. * - * @param string $name - * @param int $status - * @param string $message - * @param int $visible - * @param int $component_id - * @param int $component_status - * @param bool $notify + * @param string $name + * @param int $status + * @param string $message + * @param int $visible + * @param int $component_id + * @param int $component_status + * @param bool $notify + * @param string|null $incident_date * * @return void */ - public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify) + public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $incident_date) { $this->name = $name; $this->status = $status; @@ -99,5 +108,6 @@ class ReportIncidentCommand $this->component_id = $component_id; $this->component_status = $component_status; $this->notify = $notify; + $this->incident_date = $incident_date; } } diff --git a/app/Commands/Incident/UpdateIncidentCommand.php b/app/Commands/Incident/UpdateIncidentCommand.php index d271bca2..4e9c493b 100644 --- a/app/Commands/Incident/UpdateIncidentCommand.php +++ b/app/Commands/Incident/UpdateIncidentCommand.php @@ -71,6 +71,13 @@ class UpdateIncidentCommand */ public $notify; + /** + * The date that the incident occurred on. + * + * @var string + */ + public $incident_date; + /** * The validation rules. * @@ -97,7 +104,7 @@ class UpdateIncidentCommand * @param int $component_id * @param int $component_status * @param bool $notify - * @param string|null $incidentDate + * @param string|null $incident_date * * @return void */ diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php index 99f4e826..3d3d37e0 100644 --- a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -13,8 +13,11 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; +use Illuminate\Support\Facades\Config; +use Jenssegers\Date\Date; class ReportIncidentCommandHandler { @@ -35,6 +38,16 @@ class ReportIncidentCommandHandler 'component' => $command->component_id, ]); + // The incident occurred at a different time. + if ($command->incident_date) { + $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); + + $incident->update([ + 'created_at' => $incidentDate, + 'updated_at' => $incidentDate, + ]); + } + // Update the component. if ($command->component_id) { Component::find($command->component_id)->update([ diff --git a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php index bbaf7ac2..ef25992c 100644 --- a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php +++ b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php @@ -13,8 +13,11 @@ namespace CachetHQ\Cachet\Handlers\Commands\Incident; use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand; use CachetHQ\Cachet\Events\Incident\IncidentWasUpdatedEvent; +use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; +use Illuminate\Support\Facades\Config; +use Jenssegers\Date\Date; class UpdateIncidentCommandHandler { @@ -30,7 +33,10 @@ class UpdateIncidentCommandHandler $incident = $command->incident; $incident->update($this->filterIncidentData($command)); - if ($incidentDate = $command->incident_date) { + // The incident occurred at a different time. + if ($command->incident_date) { + $incidentDate = Date::createFromFormat('d/m/Y H:i', $command->incident_date, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); + $incident->update([ 'created_at' => $incidentDate, 'updated_at' => $incidentDate, diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 80cb5361..b5bf8e77 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -72,7 +72,8 @@ class IncidentController extends AbstractApiController Binput::get('visible', true), Binput::get('component_id'), Binput::get('component_status'), - Binput::get('notify', true) + Binput::get('notify', true), + Binput::get('created_at') )); } catch (Exception $e) { throw new BadRequestHttpException(); @@ -99,7 +100,8 @@ class IncidentController extends AbstractApiController Binput::get('visible', true), Binput::get('component_id'), Binput::get('component_status'), - Binput::get('notify', true) + Binput::get('notify', true), + Binput::get('created_at') )); } catch (Exception $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 47558194..a54a8396 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -15,7 +15,6 @@ use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand; -use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Incident; @@ -23,10 +22,8 @@ use CachetHQ\Cachet\Models\IncidentTemplate; use GrahamCampbell\Binput\Facades\Binput; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; -use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\View; -use Jenssegers\Date\Date; class IncidentController extends Controller { @@ -112,12 +109,6 @@ class IncidentController extends Controller */ public function createIncidentAction() { - $incidentDate = null; - - if ($createdAt = Binput::get('created_at')) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $createdAt, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); - } - try { $incident = $this->dispatch(new ReportIncidentCommand( Binput::get('name'), @@ -127,7 +118,7 @@ class IncidentController extends Controller Binput::get('component_id'), Binput::get('component_status'), Binput::get('notify', true), - $incidentDate + Binput::get('created_at') )); } catch (ValidationException $e) { return Redirect::route('dashboard.incidents.add') @@ -238,10 +229,6 @@ class IncidentController extends Controller */ public function editIncidentAction(Incident $incident) { - if ($createdAt = Binput::get('created_at')) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $createdAt, Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); - } - try { $incident = $this->dispatch(new UpdateIncidentCommand( $incident, @@ -251,15 +238,9 @@ class IncidentController extends Controller Binput::get('visible', true), Binput::get('component_id'), Binput::get('component_status'), - Binput::get('notify', true) + Binput::get('notify', true), + Binput::get('created_at') )); - - if (isset($incidentDate)) { - $incident->update([ - 'created_at' => $incidentDate, - 'updated_at' => $incidentDate, - ]); - } } catch (ValidationException $e) { return Redirect::route('dashboard.incidents.edit', ['id' => $incident->id]) ->withInput(Binput::all())