ReportMaintenanceCommand is added

This commit is contained in:
James Brooks
2015-08-31 20:16:15 +01:00
parent 60e9a64a3e
commit 1478a25008
5 changed files with 123 additions and 57 deletions

View File

@@ -0,0 +1,61 @@
<?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;
class ReportMaintenanceCommand
{
/**
* The maintenance name.
*
* @var string
*/
public $name;
/**
* The maintenance message.
*
* @var string
*/
public $message;
/**
* Whether to notify about the maintenance or not.
*
* @var bool
*/
public $notify;
/**
* Timestamp of when the maintenance is due to start.
*
* @var string
*/
public $timestamp;
/**
* Create a new report maintenance command instance.
*
* @param string $name
* @param string $message
* @param bool $notify
* @param string $timestamp
*
* @return void
*/
public function __construct($name, $message, $notify, $timestamp)
{
$this->name = $name;
$this->message = $message;
$this->notify = $notify;
$this->timestamp = $timestamp;
}
}

View File

@@ -1,34 +0,0 @@
<?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;
use CachetHQ\Cachet\Models\Subscriber;
class SubscriberHasVerifiedEvent
{
/**
* The subscriber who has verified.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
public $subscriber;
/**
* Create a new subscriber has subscribed event instance.
*
* @return void
*/
public function __construct(Subscriber $subscriber)
{
$this->subscriber = $subscriber;
}
}

View File

@@ -0,0 +1,51 @@
<?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\ReportMaintenanceCommand;
use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Support\Facades\Config;
use Jenssegers\Date\Date;
class ReportMaintenanceCommandHandler
{
/**
* Handle the report maintenance command.
*
* @param \CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand $command
*
* @return \CachetHQ\Cachet\Models\Incident
*/
public function handle(ReportMaintenanceCommand $command)
{
// TODO: Add validation to scheduledAt
$scheduledAt = Date::createFromFormat('d/m/Y H:i', $command->timestamp, Setting::get('app_timezone'))
->setTimezone(Config::get('app.timezone'));
$maintenanceEvent = Incident::create([
'name' => $command->name,
'message' => $command->message,
'scheduled_at' => $scheduledAt,
'status' => 0,
'visible' => 1,
]);
// Notify subscribers.
if ($command->notify) {
event(new MaintenanceWasScheduledEvent($maintenanceEvent));
}
return $maintenanceEvent;
}
}

View File

@@ -11,7 +11,7 @@
namespace CachetHQ\Cachet\Handlers\Events\Incident;
use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent;
use CachetHQ\Cachet\Events\Incident\MaintenanceWasScheduledEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;

View File

@@ -12,11 +12,12 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Events\MaintenanceWasScheduledEvent;
use CachetHQ\Cachet\Commands\Incident\ReportMaintenanceCommand;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\IncidentTemplate;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;
@@ -26,6 +27,8 @@ use Jenssegers\Date\Date;
class ScheduleController extends Controller
{
use DispatchesJobs;
/**
* Stores the sub-sidebar tree list.
*
@@ -92,24 +95,13 @@ class ScheduleController extends Controller
*/
public function addScheduleAction()
{
$scheduleData = Binput::get('incident');
// Parse the schedule date.
$scheduledAt = Date::createFromFormat('d/m/Y H:i', $scheduleData['scheduled_at'], Setting::get('app_timezone'))
->setTimezone(Config::get('app.timezone'));
if ($scheduledAt->isPast()) {
$messageBag = new MessageBag();
$messageBag->add('scheduled_at', trans('validation.date', ['attribute' => 'scheduled time you supplied']));
return Redirect::route('dashboard.schedule.add')->withErrors($messageBag);
}
$scheduleData['scheduled_at'] = $scheduledAt;
// Bypass the incident.status field.
$scheduleData['status'] = 0;
try {
$incident = Incident::create($scheduleData);
$incident = $this->dispatch(new ReportMaintenanceCommand(
Binput::get('incident.name'),
Binput::get('incident.message'),
Binput::get('incident.notify'),
Binput::get('incident.scheduled_at')
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.schedule.add')
->withInput(Binput::all())
@@ -117,10 +109,6 @@ class ScheduleController extends Controller
->withErrors($e->getMessageBag());
}
if (array_get($scheduleData, 'notify') && subscribers_enabled()) {
event(new MaintenanceWasScheduledEvent($incident));
}
return Redirect::route('dashboard.schedule.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.schedule.add.success')));
}