Specify components to subscribe to via API. Closes #1685

This commit is contained in:
James Brooks
2016-05-08 16:24:48 +01:00
parent b88a102629
commit cc102847f9
5 changed files with 59 additions and 11 deletions

View File

@@ -32,6 +32,13 @@ final class SubscribeSubscriberCommand
*/ */
public $verified; public $verified;
/**
* The list of subscriptions to set the subscriber up with.
*
* @var array|null
*/
public $subscriptions;
/** /**
* The validation rules. * The validation rules.
* *
@@ -44,14 +51,16 @@ final class SubscribeSubscriberCommand
/** /**
* Create a new subscribe subscriber command instance. * Create a new subscribe subscriber command instance.
* *
* @param string $email * @param string $email
* @param bool $verified * @param bool $verified
* @param array|null $subscriptions
* *
* @return void * @return void
*/ */
public function __construct($email, $verified = false) public function __construct($email, $verified = false, $subscriptions = null)
{ {
$this->email = $email; $this->email = $email;
$this->verified = $verified; $this->verified = $verified;
$this->subscriptions = $subscriptions;
} }
} }

View File

@@ -22,6 +22,8 @@ use CachetHQ\Cachet\Models\Subscription;
* This is the subscribe subscriber command handler. * This is the subscribe subscriber command handler.
* *
* @author James Brooks <james@alt-three.com> * @author James Brooks <james@alt-three.com>
* @author Joe Cohen <joe@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/ */
class SubscribeSubscriberCommandHandler class SubscribeSubscriberCommandHandler
{ {
@@ -40,7 +42,14 @@ class SubscribeSubscriberCommandHandler
$subscriber = Subscriber::firstOrCreate(['email' => $command->email]); $subscriber = Subscriber::firstOrCreate(['email' => $command->email]);
foreach (Component::all() as $component) { // Decide what to subscribe the subscriber to.
if ($subscriptions = $command->subscriptions) {
$subscriptions = Component::whereIn('id', $subscriptions);
} else {
$subscriptions = Component::all();
}
foreach ($subscriptions as $component) {
Subscription::create([ Subscription::create([
'subscriber_id' => $subscriber->id, 'subscriber_id' => $subscriber->id,
'component_id' => $component->id, 'component_id' => $component->id,

View File

@@ -21,6 +21,12 @@ use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* This is the subscriber controller class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class SubscriberController extends AbstractApiController class SubscriberController extends AbstractApiController
{ {
/** /**
@@ -43,7 +49,11 @@ class SubscriberController extends AbstractApiController
public function postSubscribers() public function postSubscribers()
{ {
try { try {
$subscriber = dispatch(new SubscribeSubscriberCommand(Binput::get('email'), Binput::get('verify', false), null)); $subscriber = dispatch(new SubscribeSubscriberCommand(
Binput::get('email'),
Binput::get('verify', false),
Binput::get('components', null)
));
} catch (QueryException $e) { } catch (QueryException $e) {
throw new BadRequestHttpException(); throw new BadRequestHttpException();
} }

View File

@@ -44,11 +44,11 @@ class SubscriberTest extends AbstractApiTestCase
$this->expectsEvents('CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent'); $this->expectsEvents('CachetHQ\Cachet\Bus\Events\Subscriber\SubscriberHasSubscribedEvent');
$this->post('/api/v1/subscribers', [ $this->post('/api/v1/subscribers', [
'email' => 'james@cachethq.io', 'email' => 'support@alt-three.com',
]); ]);
$this->assertResponseOk(); $this->assertResponseOk();
$this->seeHeader('Content-Type', 'application/json'); $this->seeHeader('Content-Type', 'application/json');
$this->seeJson(['email' => 'james@cachethq.io']); $this->seeJson(['email' => 'support@alt-three.com']);
} }
public function testCreateSubscriberAutoVerified() public function testCreateSubscriberAutoVerified()
@@ -56,12 +56,32 @@ class SubscriberTest extends AbstractApiTestCase
$this->beUser(); $this->beUser();
$this->post('/api/v1/subscribers', [ $this->post('/api/v1/subscribers', [
'email' => 'james@cachethq.io', 'email' => 'support@alt-three.com',
'verify' => true, 'verify' => true,
]); ]);
$this->assertResponseOk(); $this->assertResponseOk();
$this->seeHeader('Content-Type', 'application/json'); $this->seeHeader('Content-Type', 'application/json');
$this->seeJson(['email' => 'james@cachethq.io']); $this->seeJson(['email' => 'support@alt-three.com']);
}
public function testCreateSubscriberWithSubscriptions()
{
$this->beUser();
factory('CachetHQ\Cachet\Models\Component', 3)->create();
$this->post('/api/v1/subscribers', [
'email' => 'support@alt-three.com',
'verify' => true,
'subscriptions' => [
1,
2,
3,
],
]);
$this->assertResponseOk();
$this->seeHeader('Content-Type', 'application/json');
$this->seeJson(['email' => 'support@alt-three.com']);
} }
public function testDeleteSubscriber() public function testDeleteSubscriber()

View File

@@ -28,8 +28,8 @@ class SubscribeSubscriberCommandTest extends AbstractTestCase
protected function getObjectAndParams() protected function getObjectAndParams()
{ {
$params = ['email' => 'support@cachethq.io', 'verified' => true]; $params = ['email' => 'support@cachethq.io', 'verified' => true, 'subscriptions' => null];
$object = new SubscribeSubscriberCommand($params['email'], $params['verified']); $object = new SubscribeSubscriberCommand($params['email'], $params['verified'], $params['subscriptions']);
return compact('params', 'object'); return compact('params', 'object');
} }