SubscribeController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 56.47 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 1
cbo 8
dl 48
loc 85
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A showSubscribe() 0 5 1
A postSubscribe() 14 14 2
A getVerify() 17 17 4
A getUnsubscribe() 17 17 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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