Skip to content

Commit

Permalink
Merge pull request #22 from CachetHQ/2.4
Browse files Browse the repository at this point in the history
Update from upstream repo CachetHQ/Cachet
  • Loading branch information
billmn committed Oct 14, 2016
2 parents 02356fb + fe96a00 commit daaad30
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 160 deletions.
10 changes: 8 additions & 2 deletions app/Bus/Commands/Incident/ReportIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

namespace CachetHQ\Cachet\Bus\Commands\Incident;

/**
* This is the report incident command.
*
* @author Joseph Cohen <[email protected]>
* @author James Brooks <[email protected]>
*/
final class ReportIncidentCommand
{
/**
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/Bus/Commands/Incident/UpdateIncidentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
67 changes: 38 additions & 29 deletions app/Bus/Handlers/Commands/Incident/ReportIncidentCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -71,8 +63,8 @@ public function handle(ReportIncidentCommand $command)
'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;
}
Expand All @@ -94,10 +86,17 @@ public function handle(ReportIncidentCommand $command)
$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;
Expand All @@ -110,20 +109,30 @@ public function handle(ReportIncidentCommand $command)
/**
* 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 = [];
}

$this->twig->setLoader(new Twig_Loader_String());
$template = IncidentTemplate::forSlug($templateSlug)->first();

return $this->twig->render($template->template, $vars);
$env = new Twig_Environment(new Twig_Loader_Array([]));
$template = $env->createTemplate($template->template);

$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);
}
}
63 changes: 38 additions & 25 deletions app/Bus/Handlers/Commands/Incident/UpdateIncidentCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -64,8 +56,8 @@ public function __construct(DateFactory $dates, Bridge $twig)
*/
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;
Expand All @@ -82,10 +74,17 @@ public function handle(UpdateIncidentCommand $command)
}

// 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));
Expand Down Expand Up @@ -121,16 +120,30 @@ protected function filter(UpdateIncidentCommand $command)
/**
* 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();

return $this->twig->render($template->template, $vars);
$env = new Twig_Environment(new Twig_Loader_Array([]));
$template = $env->createTemplate($template->template);

$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function handle(ReportIncidentUpdateCommand $command)
null,
null,
null,
null
[]
));

event(new IncidentUpdateWasReportedEvent($update));
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Api/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function postIncidents()
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('template'),
Binput::get('vars')
Binput::get('vars', [])
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
Expand Down Expand Up @@ -109,7 +109,7 @@ public function putIncident(Incident $incident)
Binput::get('stickied', false),
Binput::get('created_at'),
Binput::get('template'),
Binput::get('vars')
Binput::get('vars', [])
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Dashboard/IncidentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function createIncidentAction()
Binput::get('stickied', false),
Binput::get('created_at'),
null,
null
[]
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.incidents.create')
Expand Down Expand Up @@ -261,7 +261,7 @@ public function editIncidentAction(Incident $incident)
Binput::get('stickied', false),
Binput::get('created_at'),
null,
null
[]
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.incidents.edit', ['id' => $incident->id])
Expand Down
23 changes: 14 additions & 9 deletions app/Models/IncidentTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +26,7 @@ class IncidentTemplate extends Model
*/
protected $casts = [
'name' => 'string',
'slug' => 'string',
'template' => 'string',
];

Expand All @@ -35,16 +35,17 @@ class IncidentTemplate extends Model
*
* @var string[]
*/
protected $fillable = ['name', 'template'];
protected $fillable = ['name', 'slug', 'template'];

/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'name' => 'required',
'template' => 'required',
'name' => 'required|string',
'slug' => 'string',
'template' => 'required|string',
];

/**
Expand All @@ -55,20 +56,24 @@ public static function boot()
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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"mccool/laravel-auto-presenter": "^4.3",
"pragmarx/google2fa": "^0.7.1",
"predis/predis": "^1.1",
"rcrowe/twigbridge": "^0.9.2",
"twig/twig": "^2.0.0",
"roumen/feed": "^2.10.4"
},
"require-dev": {
Expand Down
Loading

0 comments on commit daaad30

Please sign in to comment.