Merge pull request #866 from cachethq/commands

[WIP] First steps to Commands
This commit is contained in:
Graham Campbell
2015-09-04 12:17:57 +01:00
75 changed files with 2263 additions and 182 deletions

View File

@@ -11,16 +11,21 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class ComponentController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all components.
*
@@ -56,10 +61,15 @@ class ComponentController extends AbstractApiController
*/
public function postComponents(Guard $auth)
{
$componentData = Binput::except('tags');
try {
$component = Component::create($componentData);
$component = $this->dispatch(new AddComponentCommand(
Binput::get('name'),
Binput::get('description'),
Binput::get('status'),
Binput::get('link'),
Binput::get('order'),
Binput::get('group_id')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -119,7 +129,7 @@ class ComponentController extends AbstractApiController
*/
public function deleteComponent(Component $component)
{
$component->delete();
$this->dispatch(new RemoveComponentCommand($component));
return $this->noContent();
}

View File

@@ -11,14 +11,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Models\ComponentGroup;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class ComponentGroupController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all groups.
*
@@ -52,10 +57,11 @@ class ComponentGroupController extends AbstractApiController
*/
public function postGroups()
{
$groupData = array_filter(Binput::only(['name', 'order']));
try {
$group = ComponentGroup::create($groupData);
$group = $this->dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0)
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -77,7 +83,6 @@ class ComponentGroupController extends AbstractApiController
try {
$group->update($groupData);
} catch (Exception $e) {
dd($e->getMessage());
throw new BadRequestHttpException();
}
@@ -93,7 +98,7 @@ class ComponentGroupController extends AbstractApiController
*/
public function deleteGroup(ComponentGroup $group)
{
$group->delete();
$this->dispatch(new RemoveComponentGroupCommand($group));
return $this->noContent();
}

View File

@@ -11,16 +11,20 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Events\IncidentHasReportedEvent;
use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Models\Incident;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class IncidentController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all incidents.
*
@@ -59,30 +63,20 @@ class IncidentController extends AbstractApiController
*/
public function postIncidents(Guard $auth)
{
$incidentData = array_filter(Binput::only([
'name',
'message',
'status',
'component_id',
'notify',
'visible',
]));
// Default visibility is 1.
if (!array_has($incidentData, 'visible')) {
$incidentData['visible'] = 1;
}
try {
$incident = Incident::create($incidentData);
$incident = $this->dispatch(new ReportIncidentCommand(
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();
}
if (array_get($incidentData, 'notify') && subscribers_enabled()) {
event(new IncidentHasReportedEvent($incident));
}
return $this->item($incident);
}
@@ -122,7 +116,7 @@ class IncidentController extends AbstractApiController
*/
public function deleteIncident(Incident $incident)
{
$incident->delete();
$this->dispatch(new RemoveIncidentCommand($incident));
return $this->noContent();
}

View File

@@ -11,14 +11,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all metrics.
*
@@ -65,7 +70,15 @@ class MetricController extends AbstractApiController
public function postMetrics()
{
try {
$metric = Metric::create(Binput::all());
$metric = $this->dispatch(new AddMetricCommand(
Binput::get('name'),
Binput::get('suffix'),
Binput::get('description'),
Binput::get('default_value'),
Binput::get('calc_type', 0),
Binput::get('display_chart'),
Binput::get('places')
));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -100,7 +113,7 @@ class MetricController extends AbstractApiController
*/
public function deleteMetric(Metric $metric)
{
$metric->delete();
$this->dispatch(new RemoveMetricCommand($metric));
return $this->noContent();
}

View File

@@ -11,14 +11,20 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Commands\Metric\AddMetricPointCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricPointCommand;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use Carbon\Carbon;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricPointController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get a single metric point.
*
@@ -41,16 +47,8 @@ class MetricPointController extends AbstractApiController
*/
public function postMetricPoints(Metric $metric)
{
$metricPointData = Binput::all();
$metricPointData['metric_id'] = $metric->id;
if ($timestamp = array_pull($metricPointData, 'timestamp')) {
$pointTimestamp = Carbon::createFromFormat('U', $timestamp);
$metricPointData['created_at'] = $pointTimestamp->format('Y-m-d H:i:s');
}
try {
$metricPoint = MetricPoint::create($metricPointData);
$metricPoint = $this->dispatch(new AddMetricPointCommand($metric, Binput::get('value'), Binput::get('timestamp')));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
@@ -91,7 +89,7 @@ class MetricPointController extends AbstractApiController
*/
public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint)
{
$metricPoint->delete();
$this->dispatch(new RemoveMetricPointCommand($metricPoint));
return $this->noContent();
}

View File

@@ -11,15 +11,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Models\Subscriber;
use Exception;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class SubscriberController extends AbstractApiController
{
use DispatchesJobs;
/**
* Get all subscribers.
*
@@ -41,19 +45,12 @@ class SubscriberController extends AbstractApiController
*/
public function postSubscribers()
{
$subscriberData = Binput::except('verify');
try {
$subscriber = Subscriber::create($subscriberData);
$subscriber = $this->dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verify', false)));
} catch (Exception $e) {
throw new BadRequestHttpException();
}
// If we're auto-verifying the subscriber, don't bother with this event.
if (!(Binput::get('verify'))) {
event(new CustomerHasSubscribedEvent($subscriber));
}
return $this->item($subscriber);
}
@@ -66,7 +63,7 @@ class SubscriberController extends AbstractApiController
*/
public function deleteSubscriber(Subscriber $subscriber)
{
$subscriber->delete();
$this->dispatch(new UnsubscribeSubscriberCommand($subscriber));
return $this->noContent();
}

View File

@@ -12,16 +12,23 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\Component\AddComponentCommand;
use CachetHQ\Cachet\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\AddComponentGroupCommand;
use CachetHQ\Cachet\Commands\ComponentGroup\RemoveComponentGroupCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Tag;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class ComponentController extends Controller
{
use DispatchesJobs;
protected $subMenu = [];
public function __construct()
@@ -157,7 +164,7 @@ class ComponentController extends Controller
$tags = array_pull($_component, 'tags');
try {
$component = Component::create($_component);
$component = $this->dispatchFromArray(AddComponentCommand::class, Binput::get('component'));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.add')
->withInput(Binput::all())
@@ -188,7 +195,7 @@ class ComponentController extends Controller
*/
public function deleteComponentAction(Component $component)
{
$component->delete();
$this->dispatch(new RemoveComponentCommand($component));
return Redirect::route('dashboard.components.index');
}
@@ -202,11 +209,7 @@ class ComponentController extends Controller
*/
public function deleteComponentGroupAction(ComponentGroup $group)
{
$group->components->map(function ($component) {
$component->update(['group_id' => 0]);
});
$group->delete();
$this->dispatch(new RemoveComponentGroupCommand($group));
return Redirect::route('dashboard.components.index');
}
@@ -244,7 +247,10 @@ class ComponentController extends Controller
public function postAddComponentGroup()
{
try {
$group = ComponentGroup::create(Binput::get('group'));
$group = $this->dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0)
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.components.groups.add')
->withInput(Binput::all())

View File

@@ -12,13 +12,15 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Events\IncidentHasReportedEvent;
use CachetHQ\Cachet\Commands\Incident\RemoveIncidentCommand;
use CachetHQ\Cachet\Commands\Incident\ReportIncidentCommand;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
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;
@@ -27,6 +29,8 @@ use Jenssegers\Date\Date;
class IncidentController extends Controller
{
use DispatchesJobs;
/**
* Stores the sub-sidebar tree list.
*
@@ -107,19 +111,25 @@ class IncidentController extends Controller
*/
public function createIncidentAction()
{
$incidentData = Binput::get('incident');
$componentStatus = array_pull($incidentData, 'component_status');
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 = Incident::create($incidentData);
$incident = $this->dispatch(new ReportIncidentCommand(
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)
));
$incident->update([
'created_at' => $incidentDate,
'updated_at' => $incidentDate,
]);
} catch (ValidationException $e) {
return Redirect::route('dashboard.incidents.add')
->withInput(Binput::all())
@@ -127,15 +137,6 @@ class IncidentController extends Controller
->withErrors($e->getMessageBag());
}
// Update the component.
if (isset($incidentData['component_id']) && (int) $incidentData['component_id'] > 0) {
Component::find($incidentData['component_id'])->update(['status' => $componentStatus]);
}
if (array_get($incidentData, 'notify') && subscribers_enabled()) {
event(new IncidentHasReportedEvent($incident));
}
return Redirect::route('dashboard.incidents.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.incidents.add.success')));
}
@@ -208,7 +209,7 @@ class IncidentController extends Controller
*/
public function deleteIncidentAction(Incident $incident)
{
$incident->delete();
$this->dispatch(new RemoveIncidentCommand($incident));
return Redirect::route('dashboard.incidents.index');
}

View File

@@ -12,15 +12,20 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\Metric\AddMetricCommand;
use CachetHQ\Cachet\Commands\Metric\RemoveMetricCommand;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class MetricController extends Controller
{
use DispatchesJobs;
/**
* Shows the metrics view.
*
@@ -66,7 +71,7 @@ class MetricController extends Controller
public function createMetricAction()
{
try {
Metric::create(Binput::get('metric'));
$this->dispatchFromArray(AddMetricCommand::class, Binput::get('metric'));
} catch (ValidationException $e) {
return Redirect::route('dashboard.metrics.add')
->withInput(Binput::all())
@@ -98,7 +103,7 @@ class MetricController extends Controller
*/
public function deleteMetricAction(Metric $metric)
{
$metric->delete();
$this->dispatch(new RemoveMetricCommand($metric));
return Redirect::route('dashboard.metrics.index');
}

View File

@@ -12,11 +12,12 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Events\MaintenanceHasScheduledEvent;
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 MaintenanceHasScheduledEvent($incident));
}
return Redirect::route('dashboard.schedule.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.schedule.add.success')));
}

View File

@@ -12,15 +12,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Models\Subscriber;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class SubscriberController extends Controller
{
use DispatchesJobs;
/**
* Shows the subscribers view.
*
@@ -53,10 +57,8 @@ class SubscriberController extends Controller
*/
public function createSubscriberAction()
{
$email = Binput::get('email');
try {
$subscriber = Subscriber::create(['email' => $email]);
$this->dispatch(new SubscribeSubscriberCommand(Binput::get('email')));
} catch (ValidationException $e) {
return Redirect::route('dashboard.subscribers.add')
->withInput(Binput::all())
@@ -64,8 +66,6 @@ class SubscriberController extends Controller
->withErrors($e->getMessageBag());
}
event(new CustomerHasSubscribedEvent($subscriber));
return Redirect::route('dashboard.subscribers.add')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.subscribers.add.success')));
}
@@ -81,7 +81,7 @@ class SubscriberController extends Controller
*/
public function deleteSubscriberAction(Subscriber $subscriber)
{
$subscriber->delete();
$this->dispatch(new UnsubscribeSubscriberCommand($subscriber));
return Redirect::route('dashboard.subscribers.index');
}

View File

@@ -12,14 +12,19 @@
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Commands\User\AddTeamMemberCommand;
use CachetHQ\Cachet\Commands\User\RemoveUserCommand;
use CachetHQ\Cachet\Models\User;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
class TeamController extends Controller
{
use DispatchesJobs;
/**
* Shows the team members view.
*
@@ -65,7 +70,12 @@ class TeamController extends Controller
public function postAddUser()
{
try {
User::create(Binput::all());
$this->dispatch(new AddTeamMemberCommand(
Binput::get('username'),
Binput::get('password'),
Binput::get('email'),
Binput::get('level')
));
} catch (ValidationException $e) {
return Redirect::route('dashboard.team.add')
->withInput(Binput::except('password'))
@@ -86,19 +96,18 @@ class TeamController extends Controller
*/
public function postUpdateUser(User $user)
{
$items = Binput::all();
$passwordChange = array_get($items, 'password');
if (trim($passwordChange) === '') {
unset($items['password']);
}
$userData = array_filter(Binput::only([
'username',
'email',
'password',
'level',
]));
try {
$user->update($items);
$user->update($userData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.team.edit', ['id' => $user->id])
->withInput(Binput::except('password'))
->withInput($userData)
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure')))
->withErrors($e->getMessageBag());
}
@@ -116,7 +125,7 @@ class TeamController extends Controller
*/
public function deleteUser(User $user)
{
$user->delete();
$this->dispatch(new RemoveUserCommand($user));
return Redirect::route('dashboard.team.index')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.team.delete.success')));

View File

@@ -40,27 +40,27 @@ class UserController extends Controller
*/
public function postUser()
{
$items = Binput::all();
$userData = array_filter(Binput::only([
'username',
'email',
'password',
'google2fa',
]));
$passwordChange = array_get($items, 'password');
$enable2FA = (bool) array_pull($items, 'google2fa');
$enable2FA = (bool) array_pull($userData, 'google2fa');
// Let's enable/disable auth
if ($enable2FA && !Auth::user()->hasTwoFactor) {
$items['google_2fa_secret'] = Google2FA::generateSecretKey();
$userData['google_2fa_secret'] = Google2FA::generateSecretKey();
} elseif (!$enable2FA) {
$items['google_2fa_secret'] = '';
}
if (trim($passwordChange) === '') {
unset($items['password']);
$userData['google_2fa_secret'] = '';
}
try {
Auth::user()->update($items);
Auth::user()->update($userData);
} catch (ValidationException $e) {
return Redirect::route('dashboard.user')
->withInput(Binput::except('password'))
->withInput($userData)
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.team.edit.failure')))
->withErrors($e->getMessageBag());
}

View File

@@ -12,19 +12,24 @@
namespace CachetHQ\Cachet\Http\Controllers;
use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand;
use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Subscriber;
use Carbon\Carbon;
use GrahamCampbell\Binput\Facades\Binput;
use GrahamCampbell\Markdown\Facades\Markdown;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SubscribeController extends Controller
{
use DispatchesJobs;
/**
* Show the subscribe by email page.
*
@@ -45,7 +50,7 @@ class SubscribeController extends Controller
public function postSubscribe()
{
try {
$subscriber = Subscriber::create(['email' => Binput::get('email')]);
$this->dispatch(new SubscribeSubscriberCommand(Binput::get('email')));
} catch (ValidationException $e) {
return Redirect::route('subscribe.subscribe')
->withInput(Binput::all())
@@ -53,8 +58,6 @@ class SubscribeController extends Controller
->withErrors($e->getMessageBag());
}
event(new CustomerHasSubscribedEvent($subscriber));
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed')));
}
@@ -75,11 +78,10 @@ class SubscribeController extends Controller
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || $subscriber->verified()) {
return Redirect::route('status-page');
throw new BadRequestHttpException();
}
$subscriber->verified_at = Carbon::now();
$subscriber->save();
$this->dispatch(new VerifySubscriberCommand($subscriber));
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.verified')));
@@ -101,10 +103,10 @@ class SubscribeController extends Controller
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || !$subscriber->verified()) {
return Redirect::route('status-page');
throw new BadRequestHttpException();
}
$subscriber->delete();
$this->dispatch(new UnsubscribeSubscriberCommand($subscriber));
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.unsuscribed')));