Added ReportIncidentCommand

This commit is contained in:
James Brooks
2015-08-31 11:32:39 +01:00
parent 37d7908606
commit 082062fa1b
8 changed files with 115 additions and 58 deletions
@@ -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;
}
}
@@ -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;
@@ -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;
@@ -0,0 +1,52 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Handlers\Commands\Incident;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Events\Incident\IncidentWasReportedEvent;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
class ReportIncidentCommandHandler
{
/**
* Handle the report incident command.
*
* @param \CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand $command
*
* @return void
*/
public function handle(ReportIncidentCommand $command)
{
$incident = Incident::create([
'name' => $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;
}
}
@@ -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,
+10 -20
View File
@@ -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);
}
@@ -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')));
}