Cachet/app/Http/Controllers/SubscribeController.php

115 lines
3.5 KiB
PHP
Raw Normal View History

2015-05-24 16:33:03 -05:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
2015-05-24 16:33:03 -05:00
*
* 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;
2015-05-24 16:33:03 -05:00
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;
2015-05-24 16:33:03 -05:00
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2015-05-24 16:33:03 -05:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SubscribeController extends Controller
2015-05-24 16:33:03 -05:00
{
use DispatchesJobs;
2015-05-24 16:33:03 -05:00
/**
* 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')));
2015-05-24 16:33:03 -05:00
}
/**
* 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());
2015-05-24 16:33:03 -05:00
}
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed')));
2015-05-24 16:33:03 -05:00
}
/**
* Handle the verify subscriber email.
*
* @param string|null $code
2015-05-24 16:33:03 -05:00
*
* @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();
2015-05-24 16:33:03 -05:00
}
$this->dispatch(new VerifySubscriberCommand($subscriber));
2015-05-24 16:33:03 -05:00
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.verified')));
2015-05-24 16:33:03 -05:00
}
/**
* Handle the unsubscribe.
*
* @param string|null $code
2015-05-24 16:33:03 -05:00
*
* @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();
2015-05-24 16:33:03 -05:00
}
$this->dispatch(new UnsubscribeSubscriberCommand($subscriber));
2015-05-24 16:33:03 -05:00
return Redirect::route('status-page')
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.unsuscribed')));
2015-05-24 16:33:03 -05:00
}
}