1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gitamin\Http\Controllers; |
13
|
|
|
|
14
|
|
|
use AltThree\Validator\ValidationException; |
15
|
|
|
use Gitamin\Commands\Subscriber\SubscribeSubscriberCommand; |
16
|
|
|
use Gitamin\Commands\Subscriber\UnsubscribeSubscriberCommand; |
17
|
|
|
use Gitamin\Commands\Subscriber\VerifySubscriberCommand; |
18
|
|
|
use Gitamin\Facades\Setting; |
19
|
|
|
use Gitamin\Models\Subscriber; |
20
|
|
|
use GrahamCampbell\Markdown\Facades\Markdown; |
21
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
22
|
|
|
use Illuminate\Support\Facades\Redirect; |
23
|
|
|
use Illuminate\Support\Facades\View; |
24
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
25
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
26
|
|
|
|
27
|
|
|
class SubscribeController extends Controller |
28
|
|
|
{ |
29
|
|
|
use DispatchesJobs; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Show the subscribe by email page. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\View\View |
35
|
|
|
*/ |
36
|
|
|
public function showSubscribe() |
37
|
|
|
{ |
38
|
|
|
return View::make('subscribe') |
39
|
|
|
->withAboutApp(Markdown::convertToHtml(Setting::get('app_about'))); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Handle the subscribe user. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\View\View |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function postSubscribe() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this->dispatch(new SubscribeSubscriberCommand(Request::get('email'))); |
51
|
|
|
} catch (ValidationException $e) { |
52
|
|
|
return Redirect::route('subscribe.subscribe') |
53
|
|
|
->withInput(Request::all()) |
54
|
|
|
->withTitle(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.whoops'), trans('gitamin.subscriber.email.failure'))) |
55
|
|
|
->withErrors($e->getMessageBag()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Redirect::route('explore') |
59
|
|
|
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('gitamin.subscriber.email.subscribed'))); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handle the verify subscriber email. |
64
|
|
|
* |
65
|
|
|
* @param string|null $code |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\View\View |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function getVerify($code = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if ($code === null) { |
72
|
|
|
throw new NotFoundHttpException(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first(); |
76
|
|
|
|
77
|
|
|
if (! $subscriber || $subscriber->verified()) { |
78
|
|
|
throw new BadRequestHttpException(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->dispatch(new VerifySubscriberCommand($subscriber)); |
82
|
|
|
|
83
|
|
|
return Redirect::route('explore') |
84
|
|
|
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('gitamin.subscriber.email.verified'))); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Handle the unsubscribe. |
89
|
|
|
* |
90
|
|
|
* @param string|null $code |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\View\View |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getUnsubscribe($code = null) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
if ($code === null) { |
97
|
|
|
throw new NotFoundHttpException(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first(); |
101
|
|
|
|
102
|
|
|
if (! $subscriber || ! $subscriber->verified()) { |
103
|
|
|
throw new BadRequestHttpException(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->dispatch(new UnsubscribeSubscriberCommand($subscriber)); |
107
|
|
|
|
108
|
|
|
return Redirect::route('explore') |
109
|
|
|
->withSuccess(sprintf('<strong>%s</strong> %s', trans('dashboard.notifications.awesome'), trans('gitamin.subscriber.email.unsubscribed'))); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.