Completed
Push — master ( 1a7816...dd2067 )
by Matthew
04:09
created

QuoteController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 159
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B indexAction() 0 25 2
A randomQuoteAction() 0 19 2
B quoteAllAction() 0 30 2
B quoteAction() 0 40 4
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Symfony\Component\Debug\Exception\ContextErrorException;
8
use Symfony\Component\Routing\Exception\InvalidParameterException;
9
use Symfony\Component\HttpFoundation\Request;
10
11
// A quick note: This tool is referred to as "bash" in much of
12
// the legacy code base.  As such, the terms "quote" and
13
// "bash" are used interchangeably here, so as to not break
14
// many conventions.
15
16
class QuoteController extends Controller
17
{
18
    /**
19
     * Method for rendering the Bash Main Form.
20
     * This method redirects if valid parameters are found, making it a
21
     * valid form endpoint as well.
22
     *
23
     * @param \Symfony\Component\HttpFoundation\Request $request Given by Symfony
24
     *
25
     * @Route("/bash",  name="bash")
26
     * @Route("/quote", name="quote")
27
     *
28
     * @return \Symfony\Component\HttpFoundation\Response
29
     */
30
    public function indexAction(Request $request)
31
    {
32
        // Check if enabled
33
        $lh = $this->get("app.labs_helper");
34
        $lh->checkEnabled("bash");
35
36
        // Check to see if the quote is a param.  If so,
37
        // redirect to the proper route.
38
        if ($request->query->get('id') != '') {
39
            return $this->redirectToRoute(
40
                "quoteID",
41
                ["id"=>$request->query->get('id')]
42
            );
43
        }
44
45
        // Oterwise render the form.
46
        return $this->render(
47
            'quote/index.html.twig', [
48
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
49
            "xtPage" => "bash",
50
            "xtPageTitle" => "tool_bash",
51
            "xtSubtitle" => "tool_bash_desc",
52
            ]
53
        );
54
    }
55
56
    /**
57
     * Method for rendering a random quote.
58
     * This should redirect to the /quote/{id} path below
59
     *
60
     * @Route("/quote/random", name="quoteRandom")
61
     * @Route("/bash/random",  name="bashRandom")
62
     *
63
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
64
     */
65
    public function randomQuoteAction()
66
    {
67
        // Check if enabled
68
        $lh = $this->get("app.labs_helper");
69
        $lh->checkEnabled("bash");
70
71
        // Choose a random quote by ID.
72
        // if we can't find the quotes, return back to  the main form with
73
        // a flash notice.
74
        try {
75
            $id = rand(1, sizeof($this->getParameter("quotes")));
76
        }
77
        catch (InvalidParameterException $e) {
78
            $this->addFlash("notice", ["noquotes"]);
0 ignored issues
show
Documentation introduced by
array('noquotes') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            return $this->redirectToRoute("quote");
80
        }
81
82
        return $this->redirectToRoute("quoteID", ["id"=>$id]);
83
    }
84
85
    /**
86
     * Method to show all quotes.
87
     *
88
     * @Route("/quote/all", name="quoteAll")
89
     * @Route("/bash/all",  name="bashAll")
90
     *
91
     * @return \Symfony\Component\HttpFoundation\Response
92
     */
93
    public function quoteAllAction()
94
    {
95
        // Check if enabled
96
        $lh = $this->get("app.labs_helper");
97
        $lh->checkEnabled("bash");
98
99
        // Load up an array of all the quotes.
100
        // if we can't find the quotes, return back to  the main form with
101
        // a flash notice.
102
        try {
103
            $quotes = $this->getParameter("quotes");
104
        }
105
        catch (InvalidParameterException $e) {
106
            $this->addFlash("notice", ["noquotes"]);
0 ignored issues
show
Documentation introduced by
array('noquotes') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
            return $this->redirectToRoute("quote");
108
        }
109
110
        // Render the page.
111
        return $this->render(
112
            'quote/all.html.twig', [
113
                'base_dir' => realpath(
114
                    $this->getParameter('kernel.root_dir') . '/..'
115
                ),
116
                "xtPage" => "bash",
117
                "xtPageTitle" => "tool_bash",
118
                "xtSubtitle" => "tool_bash_desc",
119
                "quotes" => $quotes,
120
            ]
121
        );
122
    }
123
124
    /**
125
     * Method to render a single quote.
126
     *
127
     * @param int $id ID of the quote
128
     *
129
     * @Route("/quote/{id}", name="quoteID")
130
     * @Route("/bash/{id}",  name="bashID")
131
     *
132
     * @return \Symfony\Component\HttpFoundation\Response
133
     */
134
    public function quoteAction($id)
135
    {
136
        $lh = $this->get("app.labs_helper");
137
        $lh->checkEnabled("bash");
138
139
        // Get the singular quote.
140
        // if we can't find the quotes, return back to  the main form with
141
        // a flash notice.
142
        try {
143
            if (isset($this->getParameter("quotes")[$id])) {
144
                $text = $this->getParameter("quotes")[$id];
145
            } else {
146
                throw new InvalidParameterException("Quote doesn't exist");
147
            }
148
        } catch (InvalidParameterException $e) {
149
            $this->addFlash("notice", ["noquotes"]);
0 ignored issues
show
Documentation introduced by
array('noquotes') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
            return $this->redirectToRoute("quote");
151
        }
152
153
        // If the text is undefined, that quote doesn't exist.
154
        // Redirect back to the main form.
155
        if (!isset($text)) {
156
            $this->addFlash("notice", ["noquotes"]);
0 ignored issues
show
Documentation introduced by
array('noquotes') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
            return $this->redirectToRoute("quote");
158
        }
159
160
        // Show the quote.
161
        return $this->render(
162
            'quote/view.html.twig', [
163
                'base_dir' => realpath(
164
                    $this->getParameter('kernel.root_dir') . '/..'
165
                ),
166
                "xtPage" => "bash",
167
                "xtPageTitle" => "tool_bash",
168
                "xtSubtitle" => "tool_bash_desc",
169
                "text" => $text,
170
                "id" => $id,
171
            ]
172
        );
173
    }
174
}
175