Incident templates cleanup (#2182)
Clean up Incident Templates, supply incident array to them by default
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user