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

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;
}
}

View File

@@ -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,

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);
}

View File

@@ -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')));
}

View File

@@ -30,34 +30,34 @@
@endif
<div class="form-group">
<label for="incident-name">{{ trans('forms.incidents.name') }}</label>
<input type="text" class="form-control" name="incident[name]" id="incident-name" required value="{{ Input::old('incident.name') }}">
<input type="text" class="form-control" name="name" id="incident-name" required value="{{ Input::old('incident.name') }}">
</div>
<div class="form-group">
<label for="incident-name">{{ trans('forms.incidents.status') }}</label><br>
<label class="radio-inline">
<input type="radio" name="incident[status]" value="1">
<input type="radio" name="status" value="1">
<i class="icon ion-flag"></i>
{{ trans('cachet.incidents.status')[1] }}
</label>
<label class="radio-inline">
<input type="radio" name="incident[status]" value="2">
<input type="radio" name="status" value="2">
<i class="icon ion-alert-circled"></i>
{{ trans('cachet.incidents.status')[2] }}
</label>
<label class="radio-inline">
<input type="radio" name="incident[status]" value="3">
<input type="radio" name="status" value="3">
<i class="icon ion-eye"></i>
{{ trans('cachet.incidents.status')[3] }}
</label>
<label class="radio-inline">
<input type="radio" name="incident[status]" value="4">
<input type="radio" name="status" value="4">
<i class="icon ion-checkmark"></i>
{{ trans('cachet.incidents.status')[4] }}
</label>
</div>
<div class="form-group">
<label for="incident-name">{{ trans('forms.incidents.visibility') }}</label>
<select name='incident[visible]' class="form-control">
<select name='visible' class="form-control">
<option value='1' selected>{{ trans('forms.incidents.public') }}</option>
<option value='0'>{{ trans('forms.incidents.logged_in_only') }}</option>
</select>
@@ -65,7 +65,7 @@
@if(!$components_in_groups->isEmpty() || !$components_out_groups->isEmpty())
<div class="form-group">
<label>{{ trans('forms.incidents.component') }}</label>
<select name='incident[component_id]' class='form-control'>
<select name='component_id' class='form-control'>
<option value='0' selected></option>
@foreach($components_in_groups as $group)
<optgroup label="{{ $group->name }}">
@@ -88,7 +88,7 @@
@foreach(trans('cachet.components.status') as $statusID => $status)
<div class="radio-inline">
<label>
<input type="radio" name="incident[component_status]" value="{{ $statusID }}">
<input type="radio" name="component_status" value="{{ $statusID }}">
{{ $status }}
</label>
</div>
@@ -100,17 +100,17 @@
<div class="form-group">
<label>{{ trans('forms.incidents.message') }}</label>
<div class='markdown-control'>
<textarea name="incident[message]" class="form-control autosize" rows="5" required>{{ Input::old('incident.message') }}</textarea>
<textarea name="message" class="form-control autosize" rows="5" required>{{ Input::old('incident.message') }}</textarea>
</div>
</div>
<div class="form-group">
<label>{{ trans('forms.incidents.incident_time') }}</label>
<input type="text" name="incident[created_at]" class="form-control" rel="datepicker-any">
<input type="text" name="created_at" class="form-control" rel="datepicker-any">
<span class="help-block">{{ trans('forms.optional') }}</span>
</div>
<div class="form-group">
<label>{{ trans('forms.incidents.notify_subscribers') }}</label>
<input type="checkbox" name="incident[notify]" value="1" checked="{{ Input::old('incident.message', 'checked') }}">
<input type="checkbox" name="notify" value="1" checked="{{ Input::old('incident.message', 'checked') }}">
<span class="help-block">{{ trans('forms.optional') }}</span>
</div>
</fieldset>