diff --git a/app/Commands/Incident/UpdateIncidentCommand.php b/app/Commands/Incident/UpdateIncidentCommand.php new file mode 100644 index 00000000..bf6b56f3 --- /dev/null +++ b/app/Commands/Incident/UpdateIncidentCommand.php @@ -0,0 +1,99 @@ +incident = $incident; + $this->name = $name; + $this->status = $status; + $this->message = $message; + $this->visible = $visible; + $this->component_id = $component_id; + $this->component_status = $component_status; + $this->notify = $notify; + } +} diff --git a/app/Events/Incident/IncidentWasUpdatedEvent.php b/app/Events/Incident/IncidentWasUpdatedEvent.php new file mode 100644 index 00000000..7979e62b --- /dev/null +++ b/app/Events/Incident/IncidentWasUpdatedEvent.php @@ -0,0 +1,34 @@ +incident = $incident; + } +} diff --git a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php index 9487309c..b639b864 100644 --- a/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php +++ b/app/Handlers/Commands/Component/UpdateComponentCommandHandler.php @@ -34,6 +34,13 @@ class UpdateComponentCommandHandler return $component; } + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Component\UpdateComponentCommand $command + * + * @return array + */ protected function filterComponentData($command) { return array_filter([ diff --git a/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php new file mode 100644 index 00000000..03802fd7 --- /dev/null +++ b/app/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php @@ -0,0 +1,67 @@ +incident; + $incident->update($this->filterIncidentData($command)); + + // Update the component. + if ($command->component_id) { + Component::find($command->component_id)->update([ + 'status' => $command->component_status, + ]); + } + + // Notify subscribers. + if ($command->notify) { + event(new IncidentWasUpdatedEvent($incident)); + } + + return $incident; + } + + /** + * Filter the command data. + * + * @param \CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand $command + * + * @return array + */ + protected function filterIncidentData($command) + { + return array_filter([ + 'name' => $command->name, + 'status' => $command->status, + 'message' => $command->message, + 'visible' => $command->visible, + 'component_id' => $command->component_id, + 'component_status' => $command->component_status, + 'notify' => $command->notify, + ]); + } +} diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 34576b07..80cb5361 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; +use CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -89,17 +90,17 @@ class IncidentController extends AbstractApiController */ public function putIncident(Incident $incident) { - $incidentData = array_filter(Binput::only([ - 'name', - 'message', - 'status', - 'component_id', - 'notify', - 'visible', - ])); - try { - $incident->update($incidentData); + $incident = $this->dispatch(new UpdateIncidentCommand( + $incident, + Binput::get('name'), + Binput::get('status'), + Binput::get('message'), + Binput::get('visible', true), + Binput::get('component_id'), + Binput::get('component_status'), + Binput::get('notify', true) + )); } catch (Exception $e) { throw new BadRequestHttpException(); } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 64406f0c..443481fa 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -14,6 +14,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; 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; @@ -241,18 +242,28 @@ class IncidentController extends Controller */ public function editIncidentAction(Incident $incident) { - $incidentData = Binput::get('incident'); - - if (array_has($incidentData, 'created_at') && $incidentData['created_at']) { - $incidentDate = Date::createFromFormat('d/m/Y H:i', $incidentData['created_at'], Setting::get('app_timezone'))->setTimezone(Config::get('app.timezone')); - $incidentData['created_at'] = $incidentDate; - $incidentData['updated_at'] = $incidentDate; - } else { - unset($incidentData['created_at']); + 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->update($incidentData); + $incident = $this->dispatch(new UpdateIncidentCommand( + $incident, + Binput::get('name'), + Binput::get('status'), + Binput::get('message'), + Binput::get('visible', true), + Binput::get('component_id'), + Binput::get('component_status'), + Binput::get('notify', true) + )); + + 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())