Incident templates cleanup (#2182)
Clean up Incident Templates, supply incident array to them by default
This commit is contained in:
@@ -11,6 +11,12 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Bus\Commands\Incident;
|
||||
|
||||
/**
|
||||
* This is the report incident command.
|
||||
*
|
||||
* @author Joseph Cohen <joe@alt-three.com>
|
||||
* @author James Brooks <james@alt-three.com>
|
||||
*/
|
||||
final class ReportIncidentCommand
|
||||
{
|
||||
/**
|
||||
@@ -121,11 +127,11 @@ final class ReportIncidentCommand
|
||||
* @param bool $stickied
|
||||
* @param string|null $incident_date
|
||||
* @param string|null $template
|
||||
* @param array|null $template_vars
|
||||
* @param array $template_vars
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = null)
|
||||
public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = [])
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->status = $status;
|
||||
|
||||
@@ -130,11 +130,11 @@ final class UpdateIncidentCommand
|
||||
* @param bool $stickied
|
||||
* @param string|null $incident_date
|
||||
* @param string|null $template
|
||||
* @param array|null $template_vars
|
||||
* @param array $template_vars
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = null)
|
||||
public function __construct(Incident $incident, $name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $incident_date, $template, array $template_vars = [])
|
||||
{
|
||||
$this->incident = $incident;
|
||||
$this->name = $name;
|
||||
|
||||
@@ -11,14 +11,15 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
|
||||
use CachetHQ\Cachet\Bus\Commands\Incident\ReportIncidentCommand;
|
||||
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasReportedEvent;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\IncidentTemplate;
|
||||
use Twig_Loader_String;
|
||||
use TwigBridge\Bridge;
|
||||
use Twig_Environment;
|
||||
use Twig_Loader_Array;
|
||||
|
||||
/**
|
||||
* This is the report incident command handler.
|
||||
@@ -34,25 +35,16 @@ class ReportIncidentCommandHandler
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* The twig bridge instance.
|
||||
*
|
||||
* @var \TwigBridge\Bridge
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* Create a new report incident command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
* @param \TwigBridge\Bridge $twig
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates, Bridge $twig)
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,8 +63,8 @@ class ReportIncidentCommandHandler
|
||||
'stickied' => $command->stickied,
|
||||
];
|
||||
|
||||
if ($command->template) {
|
||||
$data['message'] = $this->parseIncidentTemplate($command->template, $command->template_vars);
|
||||
if ($template = IncidentTemplate::where('slug', $command->template)->first()) {
|
||||
$data['message'] = $this->parseTemplate($template, $command);
|
||||
} else {
|
||||
$data['message'] = $command->message;
|
||||
}
|
||||
@@ -94,10 +86,17 @@ class ReportIncidentCommandHandler
|
||||
$incident = Incident::create($data);
|
||||
|
||||
// Update the component.
|
||||
if ($command->component_id) {
|
||||
Component::find($command->component_id)->update([
|
||||
'status' => $command->component_status,
|
||||
]);
|
||||
if ($component = Component::find($command->component_id)) {
|
||||
dispatch(new UpdateComponentCommand(
|
||||
Component::find($command->component_id),
|
||||
null,
|
||||
null,
|
||||
$command->component_status,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
));
|
||||
}
|
||||
|
||||
$incident->notify = (bool) $command->notify;
|
||||
@@ -110,20 +109,30 @@ class ReportIncidentCommandHandler
|
||||
/**
|
||||
* Compiles an incident template into an incident message.
|
||||
*
|
||||
* @param string $templateSlug
|
||||
* @param array $vars
|
||||
* @param \CachetHQ\Cachet\Models\IncidentTemplate $template
|
||||
* @param \CachetHQ\Cachet\Bus\Commands\Incident\ReportIncidentCommand $command
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function parseIncidentTemplate($templateSlug, $vars)
|
||||
protected function parseTemplate(IncidentTemplate $template, ReportIncidentCommand $command)
|
||||
{
|
||||
if ($vars === null) {
|
||||
$vars = [];
|
||||
}
|
||||
$env = new Twig_Environment(new Twig_Loader_Array([]));
|
||||
$template = $env->createTemplate($template->template);
|
||||
|
||||
$this->twig->setLoader(new Twig_Loader_String());
|
||||
$template = IncidentTemplate::forSlug($templateSlug)->first();
|
||||
$vars = array_merge($command->template_vars, [
|
||||
'incident' => [
|
||||
'name' => $command->name,
|
||||
'status' => $command->status,
|
||||
'message' => $command->message,
|
||||
'visible' => $command->visible,
|
||||
'notify' => $command->notify,
|
||||
'stickied' => $command->stickied,
|
||||
'incident_date' => $command->incident_date,
|
||||
'component' => Component::find($command->component_id) ?: null,
|
||||
'component_status' => $command->component_status,
|
||||
],
|
||||
]);
|
||||
|
||||
return $this->twig->render($template->template, $vars);
|
||||
return $template->render($vars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,14 +11,15 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
|
||||
use CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand;
|
||||
use CachetHQ\Cachet\Bus\Events\Incident\IncidentWasUpdatedEvent;
|
||||
use CachetHQ\Cachet\Dates\DateFactory;
|
||||
use CachetHQ\Cachet\Models\Component;
|
||||
use CachetHQ\Cachet\Models\Incident;
|
||||
use CachetHQ\Cachet\Models\IncidentTemplate;
|
||||
use Twig_Loader_String;
|
||||
use TwigBridge\Bridge;
|
||||
use Twig_Environment;
|
||||
use Twig_Loader_Array;
|
||||
|
||||
/**
|
||||
* This is the update incident command handler.
|
||||
@@ -34,25 +35,16 @@ class UpdateIncidentCommandHandler
|
||||
*/
|
||||
protected $dates;
|
||||
|
||||
/**
|
||||
* The twig bridge instance.
|
||||
*
|
||||
* @var \TwigBridge\Bridge
|
||||
*/
|
||||
protected $twig;
|
||||
|
||||
/**
|
||||
* Create a new update incident command handler instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Dates\DateFactory $dates
|
||||
* @param \TwigBridge\Bridge $twig
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(DateFactory $dates, Bridge $twig)
|
||||
public function __construct(DateFactory $dates)
|
||||
{
|
||||
$this->dates = $dates;
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,8 +56,8 @@ class UpdateIncidentCommandHandler
|
||||
*/
|
||||
public function handle(UpdateIncidentCommand $command)
|
||||
{
|
||||
if ($command->template) {
|
||||
$command->message = $this->parseIncidentTemplate($command->template, $command->template_vars);
|
||||
if ($template = IncidentTemplate::where('slug', $command->template)->first()) {
|
||||
$command->message = $this->parseTemplate($template, $command);
|
||||
}
|
||||
|
||||
$incident = $command->incident;
|
||||
@@ -82,10 +74,17 @@ class UpdateIncidentCommandHandler
|
||||
}
|
||||
|
||||
// Update the component.
|
||||
if ($command->component_id) {
|
||||
Component::find($command->component_id)->update([
|
||||
'status' => $command->component_status,
|
||||
]);
|
||||
if ($component = Component::find($command->component_id)) {
|
||||
dispatch(new UpdateComponentCommand(
|
||||
Component::find($command->component_id),
|
||||
null,
|
||||
null,
|
||||
$command->component_status,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
));
|
||||
}
|
||||
|
||||
event(new IncidentWasUpdatedEvent($incident));
|
||||
@@ -121,16 +120,30 @@ class UpdateIncidentCommandHandler
|
||||
/**
|
||||
* Compiles an incident template into an incident message.
|
||||
*
|
||||
* @param string $templateSlug
|
||||
* @param array $vars
|
||||
* @param \CachetHQ\Cachet\Models\IncidentTemplate $template
|
||||
* @param \CachetHQ\Cachet\Bus\Commands\Incident\UpdateIncidentCommand $command
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function parseIncidentTemplate($templateSlug, $vars)
|
||||
protected function parseTemplate(IncidentTemplate $template, UpdateIncidentCommand $command)
|
||||
{
|
||||
$this->twig->setLoader(new Twig_Loader_String());
|
||||
$template = IncidentTemplate::forSlug($templateSlug)->first();
|
||||
$env = new Twig_Environment(new Twig_Loader_Array([]));
|
||||
$template = $env->createTemplate($template->template);
|
||||
|
||||
return $this->twig->render($template->template, $vars);
|
||||
$vars = array_merge($command->template_vars, [
|
||||
'incident' => [
|
||||
'name' => $command->name,
|
||||
'status' => $command->status,
|
||||
'message' => $command->message,
|
||||
'visible' => $command->visible,
|
||||
'notify' => $command->notify,
|
||||
'stickied' => $command->stickied,
|
||||
'incident_date' => $command->incident_date,
|
||||
'component' => Component::find($command->component_id) ?: null,
|
||||
'component_status' => $command->component_status,
|
||||
],
|
||||
]);
|
||||
|
||||
return $template->render($vars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ class ReportIncidentUpdateCommandHandler
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
[]
|
||||
));
|
||||
|
||||
event(new IncidentUpdateWasReportedEvent($update));
|
||||
|
||||
@@ -78,7 +78,7 @@ class IncidentController extends AbstractApiController
|
||||
Binput::get('stickied', false),
|
||||
Binput::get('created_at'),
|
||||
Binput::get('template'),
|
||||
Binput::get('vars')
|
||||
Binput::get('vars', [])
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
@@ -109,7 +109,7 @@ class IncidentController extends AbstractApiController
|
||||
Binput::get('stickied', false),
|
||||
Binput::get('created_at'),
|
||||
Binput::get('template'),
|
||||
Binput::get('vars')
|
||||
Binput::get('vars', [])
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
|
||||
@@ -135,7 +135,7 @@ class IncidentController extends Controller
|
||||
Binput::get('stickied', false),
|
||||
Binput::get('created_at'),
|
||||
null,
|
||||
null
|
||||
[]
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return cachet_redirect('dashboard.incidents.create')
|
||||
@@ -261,7 +261,7 @@ class IncidentController extends Controller
|
||||
Binput::get('stickied', false),
|
||||
Binput::get('created_at'),
|
||||
null,
|
||||
null
|
||||
[]
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return cachet_redirect('dashboard.incidents.edit', ['id' => $incident->id])
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
namespace CachetHQ\Cachet\Models;
|
||||
|
||||
use AltThree\Validator\ValidatingTrait;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -27,6 +26,7 @@ class IncidentTemplate extends Model
|
||||
*/
|
||||
protected $casts = [
|
||||
'name' => 'string',
|
||||
'slug' => 'string',
|
||||
'template' => 'string',
|
||||
];
|
||||
|
||||
@@ -35,7 +35,7 @@ class IncidentTemplate extends Model
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected $fillable = ['name', 'template'];
|
||||
protected $fillable = ['name', 'slug', 'template'];
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
@@ -43,8 +43,9 @@ class IncidentTemplate extends Model
|
||||
* @var string[]
|
||||
*/
|
||||
public $rules = [
|
||||
'name' => 'required',
|
||||
'template' => 'required',
|
||||
'name' => 'required|string',
|
||||
'slug' => 'string',
|
||||
'template' => 'required|string',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -55,20 +56,24 @@ class IncidentTemplate extends Model
|
||||
parent::boot();
|
||||
|
||||
self::saving(function ($template) {
|
||||
$template->slug = Str::slug($template->name);
|
||||
if (!$template->slug) {
|
||||
$template->slug = Str::slug($template->name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a template by the slug.
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query
|
||||
* @param string $slug
|
||||
* @param string $slug
|
||||
* @param string[] $columns
|
||||
*
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
public function scopeForSlug(Builder $query, $slug)
|
||||
public static function forSlug($slug, $columns = ['*'])
|
||||
{
|
||||
return $query->where('slug', $slug);
|
||||
$template = static::where('slug', $slug)->firstOrFail($columns);
|
||||
|
||||
return $template;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user