Subscribe and unsubscribe subscribers via commands

This commit is contained in:
Joseph Cohen
2015-08-06 21:16:49 -05:00
committed by James Brooks
parent 11b4ab5c6c
commit 8250e4ebca
16 changed files with 311 additions and 39 deletions

View File

@@ -0,0 +1,52 @@
<?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\Subscriber;
class SubscribeSubscriberCommand
{
/**
* The subscriber email.
*
* @var string
*/
public $email;
/**
* The subscriber auto verification.
*
* @var string
*/
public $verified;
/**
* The validation rules.
*
* @var array
*/
public $rules = [
'email' => 'required|email',
];
/**
* Create a new subscribe subscriber command instance.
*
* @param string $email
* @param bool $verified
*
* @return void
*/
public function __construct($email, $verified = false)
{
$this->email = $email;
$this->verified = $verified;
}
}

View File

@@ -0,0 +1,36 @@
<?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\Subscriber;
use CachetHQ\Cachet\Models\Subscriber;
class UnsubscribeSubscriberCommand
{
/**
* The subscriber to unsubscribe.
*
* @var string
*/
public $subscriber;
/**
* Create a unsubscribe subscriber command instance.
*
* @param string $subscriber
*
* @return void
*/
public function __construct(Subscriber $subscriber)
{
$this->subscriber = $subscriber;
}
}

View File

@@ -11,24 +11,26 @@
namespace CachetHQ\Cachet\Commands\Subscriber;
class SubscribeCustomerCommand
use CachetHQ\Cachet\Models\Subscriber;
class VerifySubscriberCommand
{
/**
* The customer email.
* The subscriber to verify.
*
* @var string
*/
public $email;
public $subscriber;
/**
* Create a new subscribe customer command instance.
* Create a verify subscriber command instance.
*
* @param string $email
* @param string $subscriber
*
* @return void
*/
public function __construct($email)
public function __construct(Subscriber $subscriber)
{
$this->email = $email;
$this->subscriber = $subscriber;
}
}

View File

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

View File

@@ -0,0 +1,32 @@
<?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 SubscriberHasUnsubscribedEvent
{
/**
* The subscriber who has unsubscribed.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
public $subscriber;
/**
* Create a new subscriber has unsubscribed event instance.
*/
public function __construct(Subscriber $subscriber)
{
$this->subscriber = $subscriber;
}
}

View File

@@ -13,17 +13,17 @@ namespace CachetHQ\Cachet\Events;
use CachetHQ\Cachet\Models\Subscriber;
class CustomerHasSubscribedEvent
class SubscriberHasVerifiedEvent
{
/**
* The customer who has subscribed.
* The subscriber who has verified.
*
* @var \CachetHQ\Cachet\Models\Subscriber
*/
public $subscriber;
/**
* Create a new customer has subscribed event instance.
* Create a new subscriber has subscribed event instance.
*
* @return void
*/

View File

@@ -0,0 +1,43 @@
<?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\Subscriber;
use CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand;
use CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Foundation\Bus\DispatchesJobs;
class SubscribeSubscriberCommandHandler
{
use DispatchesJobs;
/**
* Handle the subscribe subscriber command.
*
* @param \CachetHQ\Cachet\Commands\Subscriber\SubscribeSubscriberCommand $command
*
* @return \CachetHQ\Cachet\Models\Subscriber
*/
public function handle(SubscribeSubscriberCommand $command)
{
$subscriber = Subscriber::create(['email' => $command->email]);
if ($command->verified) {
$this->dispatch(new VerifySubscriberCommand($subscriber));
} else {
event(new SubscriberHasSubscribedEvent($subscriber));
}
return $subscriber;
}
}

View File

@@ -0,0 +1,35 @@
<?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\Subscriber;
use CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Events\SubscriberHasUnsubscribedEvent;
use CachetHQ\Cachet\Models\Subscriber;
class UnsubscribeSubscriberCommandHandler
{
/**
* Handle the subscribe customer command.
*
* @param \CachetHQ\Cachet\Commands\Subscriber\UnsubscribeSubscriberCommand $command
*
* @return void
*/
public function handle(UnsubscribeSubscriberCommand $command)
{
$subscriber = $command->subscriber;
event(new SubscriberHasUnsubscribedEvent($subscriber));
$subscriber->delete();
}
}

View File

@@ -0,0 +1,37 @@
<?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\Subscriber;
use CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand;
use CachetHQ\Cachet\Events\SubscriberHasVerifiedEvent;
use CachetHQ\Cachet\Models\Subscriber;
use Carbon\Carbon;
class VerifySubscriberCommandHandler
{
/**
* Handle the subscribe customer command.
*
* @param \CachetHQ\Cachet\Commands\Subscriber\VerifySubscriberCommand $command
*
* @return void
*/
public function handle(VerifySubscriberCommand $command)
{
$subscriber = $command->subscriber;
$subscriber->verified_at = Carbon::now();
$subscriber->save();
event(new SubscriberHasVerifiedEvent($subscriber));
}
}

View File

@@ -11,7 +11,7 @@
namespace CachetHQ\Cachet\Handlers\Commands\User;
use CachetHQ\Cachet\Commands\GenerateApiTokenCommand;
use CachetHQ\Cachet\Commands\User\GenerateApiTokenCommand;
use CachetHQ\Cachet\Models\User;
class GenerateApiTokenCommandHandler
@@ -19,7 +19,7 @@ class GenerateApiTokenCommandHandler
/**
* Handle the generate api key command.
*
* @param \CachetHQ\Cachet\Commands\GenerateApiTokenCommand $command
* @param \CachetHQ\Cachet\Commands\User\GenerateApiTokenCommand $command
*
* @return void
*/

View File

@@ -11,7 +11,7 @@
namespace CachetHQ\Cachet\Handlers\Events;
use CachetHQ\Cachet\Events\CustomerHasSubscribedEvent;
use CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent;
use Illuminate\Contracts\Mail\MailQueue;
use Illuminate\Mail\Message;
@@ -43,7 +43,7 @@ class SendSubscriberVerificationEmailHandler
*
* @return void
*/
public function handle(CustomerHasSubscribedEvent $event)
public function handle(SubscriberHasSubscribedEvent $event)
{
$mail = [
'email' => $event->subscriber->email,

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('verified', 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,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.
*
@@ -56,7 +60,7 @@ class SubscriberController extends Controller
$email = Binput::get('email');
try {
$subscriber = Subscriber::create(['email' => $email]);
$this->dispatch(new SubscribeSubscriberCommand($email));
} catch (ValidationException $e) {
return Redirect::route('dashboard.subscribers.add')
->withInput(Binput::all())
@@ -81,7 +85,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,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')));

View File

@@ -25,7 +25,7 @@ class AppServiceProvider extends ServiceProvider
public function boot(Dispatcher $dispatcher)
{
$dispatcher->mapUsing(function ($command) {
return Dispatcher::simpleMapping($command, 'CachetHQ\Cachet\Commands', 'CachetHQ\Cachet\Handlers\Commands');
return Dispatcher::simpleMapping($command, 'CachetHQ\Cachet', 'CachetHQ\Cachet\Handlers');
});
Str::macro('canonicalize', function ($url) {

View File

@@ -21,7 +21,7 @@ class EventServiceProvider extends ServiceProvider
* @var array
*/
protected $listen = [
'CachetHQ\Cachet\Events\CustomerHasSubscribedEvent' => [
'CachetHQ\Cachet\Events\SubscriberHasSubscribedEvent' => [
'CachetHQ\Cachet\Handlers\Events\SendSubscriberVerificationEmailHandler',
],
'CachetHQ\Cachet\Events\IncidentHasReportedEvent' => [