Cachet/app/Http/Controllers/Api/SubscriberController.php

91 lines
2.5 KiB
PHP
Raw Normal View History

<?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\Http\Controllers\Api;
2016-01-05 02:38:07 +00:00
use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand;
use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriberCommand;
use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriptionCommand;
use CachetHQ\Cachet\Models\Subscriber;
use CachetHQ\Cachet\Models\Subscription;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Config\Repository;
2015-11-21 21:18:40 +00:00
use Illuminate\Database\QueryException;
2015-12-23 14:51:18 +00:00
use Illuminate\Support\Facades\Request;
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
{
/**
* Get all subscribers.
*
2015-08-06 11:44:01 +01:00
* @return \Illuminate\Http\JsonResponse
*/
2015-12-23 14:51:18 +00:00
public function getSubscribers()
{
$subscribers = Subscriber::paginate(Binput::get('per_page', 20));
2015-12-23 14:51:18 +00:00
return $this->paginator($subscribers, Request::instance());
}
/**
* Create a new subscriber.
*
2015-08-06 11:44:01 +01:00
* @return \Illuminate\Http\JsonResponse
*/
public function postSubscribers()
{
2016-07-20 19:54:48 +01:00
$verified = Binput::get('verify', app(Repository::class)->get('setting.skip_subscriber_verification'));
2016-07-20 19:52:23 +01:00
try {
2016-07-20 19:52:23 +01:00
$subscriber = dispatch(new SubscribeSubscriberCommand(Binput::get('email'), $verified, Binput::get('components')));
2015-11-21 21:18:40 +00:00
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
return $this->item($subscriber);
}
/**
* Delete a subscriber.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return \Illuminate\Http\JsonResponse
*/
public function deleteSubscriber(Subscriber $subscriber)
{
2015-12-24 15:16:09 +00:00
dispatch(new UnsubscribeSubscriberCommand($subscriber));
return $this->noContent();
}
/**
* Delete a subscriber.
*
* @param \CachetHQ\Cachet\Models\Subscriber $subscriber
*
* @return \Illuminate\Http\JsonResponse
*/
public function deleteSubscription(Subscription $subscriber)
{
dispatch(new UnsubscribeSubscriptionCommand($subscriber));
return $this->noContent();
}
}