Cachet/app/Http/Controllers/SubscribeController.php

115 lines
3.5 KiB
PHP

<?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;
use AltThree\Validator\ValidationException;
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 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.
*
* @return \Illuminate\View\View
*/
public function showSubscribe()
{
return View::make('subscribe')
->withPageTitle(Setting::get('app_name'))
->withAboutApp(Markdown::convertToHtml(Setting::get('app_about')));
}
/**
* Handle the subscribe user.
*
* @return \Illuminate\View\View
*/
public function postSubscribe()
{
try {
$this->dispatch(new SubscribeSubscriberCommand(Binput::get('email')));
} catch (ValidationException $e) {
return Redirect::route('subscribe.subscribe')
->withInput(Binput::all())
->withTitle(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.whoops'), trans('cachet.subscriber.email.failure')))
->withErrors($e->getMessageBag());
}
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed')));
}
/**
* Handle the verify subscriber email.
*
* @param string|null $code
*
* @return \Illuminate\View\View
*/
public function getVerify($code = null)
{
if (is_null($code)) {
throw new NotFoundHttpException();
}
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || $subscriber->verified()) {
throw new BadRequestHttpException();
}
$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')));
}
/**
* Handle the unsubscribe.
*
* @param string|null $code
*
* @return \Illuminate\View\View
*/
public function getUnsubscribe($code = null)
{
if (is_null($code)) {
throw new NotFoundHttpException();
}
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
if (!$subscriber || !$subscriber->verified()) {
throw new BadRequestHttpException();
}
$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')));
}
}