Added UpdateIncidentCommand

This commit is contained in:
James Brooks 2015-09-22 19:48:08 +01:00
parent 30b05f360d
commit b8b81f7e1c
6 changed files with 238 additions and 19 deletions

View File

@ -0,0 +1,99 @@
<?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\Commands\Incident;
use CachetHQ\Cachet\Models\Incident;
class UpdateIncidentCommand
{
/**
* The incident.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
public $incident;
/**
* The incident name.
*
* @var string
*/
public $name;
/**
* The incident status.
*
* @var int
*/
public $status;
/**
* The incident message.
*
* @var string
*/
public $message;
/**
* The incident visibility.
*
* @var int
*/
public $visible;
/**
* The incident component.
*
* @var int
*/
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 update incident command instance.
*
* @param \CachetHQ\Cachet\Models\Incident $name
* @param string $name
* @param int $status
* @param string $message
* @param int $visible
* @param int $component_id
* @param int $component_status
* @param bool $notify
*
* @return void
*/
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify)
{
$this->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;
}
}

View File

@ -0,0 +1,34 @@
<?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\Events\Incident;
use CachetHQ\Cachet\Models\Incident;
class IncidentWasUpdatedEvent
{
/**
* The incident that has been updated.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
public $incident;
/**
* Create a new incident has updated event instance.
*
* @return void
*/
public function __construct(Incident $incident)
{
$this->incident = $incident;
}
}

View File

@ -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([

View File

@ -0,0 +1,67 @@
<?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\UpdateIncidentCommand;
use CachetHQ\Cachet\Events\Incident\IncidentWasUpdatedEvent;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Incident;
class UpdateIncidentCommandHandler
{
/**
* Handle the update incident command.
*
* @param \CachetHQ\Cachet\Commands\Incident\UpdateIncidentCommand $command
*
* @return \CachetHQ\Cachet\Models\Incident
*/
public function handle(UpdateIncidentCommand $command)
{
$incident = $command->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,
]);
}
}

View File

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

View File

@ -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())