From 082062fa1b7bb8b9e5da575f7e2f9edc02e7ac2c Mon Sep 17 00:00:00 2001 From: James Brooks Date: Mon, 31 Aug 2015 11:32:39 +0100 Subject: [PATCH] Added ReportIncidentCommand --- .../Incident/ReportIncidentCommand.php | 26 ++++++++-- .../Incident/IncidentWasReportedEvent.php | 2 +- .../Incident/MaintenanceWasScheduledEvent.php | 2 +- .../Incident/ReportIncidentCommandHandler.php | 52 +++++++++++++++++++ .../SendIncidentEmailNotificationHandler.php | 2 +- .../Controllers/Api/IncidentController.php | 30 ++++------- .../Dashboard/IncidentController.php | 37 ++++++------- .../views/dashboard/incidents/add.blade.php | 22 ++++---- 8 files changed, 115 insertions(+), 58 deletions(-) create mode 100644 app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php diff --git a/app/Commands/Incident/ReportIncidentCommand.php b/app/Commands/Incident/ReportIncidentCommand.php index 3bf1e000..073e183a 100644 --- a/app/Commands/Incident/ReportIncidentCommand.php +++ b/app/Commands/Incident/ReportIncidentCommand.php @@ -46,7 +46,21 @@ class ReportIncidentCommand * * @var int */ - public $component; + public $component_id; + + /** + * The component status. + * + * @var int + */ + public $component_status; + + /** + * Whether to notify about the incident or not. + * + * @var bool + */ + public $notify; /** * Create a new report incident command instance. @@ -55,16 +69,20 @@ class ReportIncidentCommand * @param int $status * @param string $message * @param int $visible - * @param int $component + * @param int $component_id + * @param int $component_status + * @param bool $notify * * @return void */ - public function __construct($name, $status, $message, $visible, $component) + public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify) { $this->name = $name; $this->status = $status; $this->message = $message; $this->visible = $visible; - $this->component = $component; + $this->component_id = $component_id; + $this->component_status = $component_status; + $this->notify = $notify; } } diff --git a/app/Events/Incident/IncidentWasReportedEvent.php b/app/Events/Incident/IncidentWasReportedEvent.php index 82e4fa6a..3c15e0bf 100644 --- a/app/Events/Incident/IncidentWasReportedEvent.php +++ b/app/Events/Incident/IncidentWasReportedEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; diff --git a/app/Events/Incident/MaintenanceWasScheduledEvent.php b/app/Events/Incident/MaintenanceWasScheduledEvent.php index b7b7f079..fcd0aaaf 100644 --- a/app/Events/Incident/MaintenanceWasScheduledEvent.php +++ b/app/Events/Incident/MaintenanceWasScheduledEvent.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace CachetHQ\Cachet\Events; +namespace CachetHQ\Cachet\Events\Incident; use CachetHQ\Cachet\Models\Incident; diff --git a/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php new file mode 100644 index 00000000..bd0a01a8 --- /dev/null +++ b/app/Handlers/Commands/Incident/ReportIncidentCommandHandler.php @@ -0,0 +1,52 @@ + $command->name, + 'status' => $command->status, + 'message' => $command->message, + 'visible' => $command->visible, + 'component' => $command->component_id, + ]); + + // Update the component. + if ($command->component_id) { + Component::find($command->component_id)->update([ + 'status' => $command->component_status, + ]); + } + + // Notify subscribers. + if ($command->notify) { + event(new IncidentWasReportedEvent($incident)); + } + + return $incident; + } +} diff --git a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php index 0421ec14..f991b08f 100644 --- a/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php +++ b/app/Handlers/Events/Incident/SendIncidentEmailNotificationHandler.php @@ -75,7 +75,7 @@ class SendIncidentEmailNotificationHandler 'email' => $subscriber->email, 'subject' => 'New incident reported.', 'has_component' => ($event->incident->component) ? true : false, - 'component_name' => $component->name, + 'component_name' => $component ? $component->name : null, 'status' => $incident->humanStatus, 'html_content' => $incident->formattedMessage, 'text_content' => $incident->message, diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 731aa847..34576b07 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -12,7 +12,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; -use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Models\Incident; use Exception; use GrahamCampbell\Binput\Facades\Binput; @@ -63,30 +63,20 @@ class IncidentController extends AbstractApiController */ public function postIncidents(Guard $auth) { - $incidentData = array_filter(Binput::only([ - 'name', - 'message', - 'status', - 'component_id', - 'notify', - 'visible', - ])); - - // Default visibility is 1. - if (!array_has($incidentData, 'visible')) { - $incidentData['visible'] = 1; - } - try { - $incident = Incident::create($incidentData); + $incident = $this->dispatch(new ReportIncidentCommand( + 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(); } - if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentWasReportedEvent($incident)); - } - return $this->item($incident); } diff --git a/app/Http/Controllers/Dashboard/IncidentController.php b/app/Http/Controllers/Dashboard/IncidentController.php index 1b16aaf4..b8ea80ca 100644 --- a/app/Http/Controllers/Dashboard/IncidentController.php +++ b/app/Http/Controllers/Dashboard/IncidentController.php @@ -13,7 +13,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Dashboard; use AltThree\Validator\ValidationException; use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand; -use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent; +use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand; use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; @@ -111,19 +111,25 @@ class IncidentController extends Controller */ public function createIncidentAction() { - $incidentData = Binput::get('incident'); - $componentStatus = array_pull($incidentData, 'component_status'); - - 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 = Incident::create($incidentData); + $incident = $this->dispatch(new ReportIncidentCommand( + 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) + )); + + $incident->update([ + 'created_at' => $incidentDate, + 'updated_at' => $incidentDate, + ]); } catch (ValidationException $e) { return Redirect::route('dashboard.incidents.add') ->withInput(Binput::all()) @@ -131,15 +137,6 @@ class IncidentController extends Controller ->withErrors($e->getMessageBag()); } - // Update the component. - if (isset($incidentData['component_id']) && (int) $incidentData['component_id'] > 0) { - Component::find($incidentData['component_id'])->update(['status' => $componentStatus]); - } - - if (array_get($incidentData, 'notify') && subscribers_enabled()) { - event(new IncidentWasReportedEvent($incident)); - } - return Redirect::route('dashboard.incidents.add') ->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.add.success'))); } diff --git a/resources/views/dashboard/incidents/add.blade.php b/resources/views/dashboard/incidents/add.blade.php index b425105d..00f4ef45 100644 --- a/resources/views/dashboard/incidents/add.blade.php +++ b/resources/views/dashboard/incidents/add.blade.php @@ -30,34 +30,34 @@ @endif
- +

- @@ -65,7 +65,7 @@ @if(!$components_in_groups->isEmpty() || !$components_out_groups->isEmpty())
- @foreach($components_in_groups as $group) @@ -88,7 +88,7 @@ @foreach(trans('cachet.components.status') as $statusID => $status)
@@ -100,17 +100,17 @@
- +
- + {{ trans('forms.optional') }}
- + {{ trans('forms.optional') }}