Passed
Push — master ( 35c320...7deb12 )
by MusikAnimal
04:42
created

RfXAnalysisController::resultAction()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 84
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 54
nc 8
nop 3
dl 0
loc 84
ccs 0
cts 0
cp 0
crap 42
rs 8.35
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file contains the code that powers the RfX Analysis page of XTools.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Xtools\ProjectRepository;
13
use Xtools\Page;
14
use Xtools\PageRepository;
15
use Xtools\UserRepository;
16
use Xtools\RFX;
17
18
/**
19
 * This controller handles the RfX Analysis tool.
20
 */
21
class RfXAnalysisController extends Controller
22
{
23
24
    /**
25
     * Get the name of the tool's index route.
26
     * @return string
27
     * @codeCoverageIgnore
28
     */
29
    public function getIndexRoute()
30
    {
31
        return 'RfXAnalysis';
32
    }
33
34
    /**
35
     * Renders the index page for the RfX Tool
36
     *
37
     * @param Request $request Given by Symfony
38
     * @param string  $project Optional project.
39
     * @param string  $type    Optional RfX type
40
     *
41
     * @Route("/rfx",                  name="RfXAnalysis")
42
     * @Route("/rfx/",                 name="RfXAnalysisSlash")
43
     * @Route("/rfx/index.php",        name="rfxAnalysisIndexPhp")
44
     * @Route("/rfx/{project}",        name="rfxAnalysisProject")
45
     * @Route("/rfx/{project}/{type}", name="rfxAnalysisProjectType")
46
     *
47
     * @return Response|RedirectResponse
0 ignored issues
show
Bug introduced by
The type AppBundle\Controller\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type AppBundle\Controller\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
     */
49
    public function indexAction(Request $request, $project = null, $type = null)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function indexAction(Request $request, $project = null, /** @scrutinizer ignore-unused */ $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $project is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function indexAction(Request $request, /** @scrutinizer ignore-unused */ $project = null, $type = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        if ($request->get('projecttype')
52
            && (strpos($request->get('projecttype'), '|') !== false)
53
        ) {
54
            $projectType = explode('|', $request->get('projecttype'), 2);
55
            $projectQuery = $projectType[0];
56
            $typeQuery = $projectType[1];
57
        } else {
58
            $projectQuery = $request->get('project');
59
            $typeQuery = $request->get('type');
60
        }
61
62
        $username = $request->get('username');
63
64
        if ($projectQuery != '' && $typeQuery != '' && $username != '') {
65
            return $this->redirectToRoute(
66
                'rfxAnalysisResult',
67
                [
68
                    'project' => $projectQuery,
69
                    'type' => $typeQuery,
70
                    'username' => $username
71
                ]
72
            );
73
        } elseif ($projectQuery != '' && $typeQuery != '') {
74
            return $this->redirectToRoute(
75
                'rfxAnalysisProjectType',
76
                [
77
                    'project' => $projectQuery,
78
                    'type' => $typeQuery
79
                ]
80
            );
81
        }
82
83
        $rfx = $this->getParameter('rfx');
84
85
        $projectFields = [];
86
87
        foreach (array_keys($rfx) as $row) {
88
            $projectFields[$row] = $rfx[$row]['pages'];
89
        }
90
91
        // replace this example code with whatever you need
92
        return $this->render(
93
            'rfxAnalysis/index.html.twig',
94
            [
95
                'xtPageTitle' => 'tool-rfx',
96
                'xtSubtitle' => 'tool-rfx-desc',
97
                'xtPage' => 'rfx',
98
                'project' => $projectQuery,
99
                'available' => $projectFields,
100
            ]
101
        );
102
    }
103
104
    /**
105
     * Renders the output page for the RfX Tool
106
     * @Route("/rfx/{project}/{type}/{username}", name="rfxAnalysisResult")
107
     * @param string $project  Optional project.
108
     * @param string $type     Type of RfX we are processing.
109
     * @param string $username Username of the person we're analizing.
110
     * @return Response|RedirectResponse
111
     * @codeCoverageIgnore
112
     */
113
    public function resultAction($project, $type, $username)
114
    {
115
        $projectData = ProjectRepository::getProject($project, $this->container);
116
117
        if (!$projectData->exists()) {
118
            $this->addFlash('notice', ['invalid-project', $project]);
0 ignored issues
show
Bug introduced by
array('invalid-project', $project) of type array<integer,string> is incompatible with the type string expected by parameter $message of Symfony\Bundle\Framework...\Controller::addFlash(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            $this->addFlash('notice', /** @scrutinizer ignore-type */ ['invalid-project', $project]);
Loading history...
119
            return $this->redirectToRoute('rfx');
120
        }
121
122
        $db = $projectData->getDatabaseName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db is dead and can be removed.
Loading history...
123
        $domain = $projectData->getDomain();
124
125
        if ($this->getParameter('rfx')[$domain] === null) {
126
            $this->addFlash('notice', ['invalid-project-cant-use', $project]);
127
            return $this->redirectToRoute('rfx');
128
        }
129
130
        // Construct the page name
131
        if (!isset($this->getParameter('rfx')[$domain]['pages'][$type])) {
132
            $pagename = '';
133
        } else {
134
            $pagename = $this->getParameter('rfx')[$domain]['pages'][$type];
135
        }
136
137
        $user = UserRepository::getUser($username, $this->container);
138
139
        $pagename .= '/'.$user->getUsername();
140
        $page = new Page($projectData, $pagename);
141
        $pageRepo = new PageRepository();
142
        $pageRepo->setContainer($this->container);
143
        $page->setRepository($pageRepo);
144
145
        $text = $page->getWikitext();
146
147
        if (!isset($text)) {
148
            $this->addFlash('notice', ['no-result', $pagename]);
149
            return $this->redirectToRoute(
150
                'rfxAnalysisProject',
151
                [
152
                    'project' => $projectData->getDatabaseName()
153
                ]
154
            );
155
        }
156
157
        $rfx = new RFX(
158
            $text,
159
            $this->getParameter('rfx')[$domain]['sections'],
160
            'User'
161
        );
162
        $support = $rfx->getSection('support');
163
        $oppose = $rfx->getSection('oppose');
164
        $neutral = $rfx->getSection('neutral');
165
        $dup = $rfx->getDuplicates();
166
167
        $total = count($support) + count($oppose) + count($neutral);
168
169
        if ($total === 0) {
170
            $this->addFlash('notice', ['no-result', $pagename]);
171
            return $this->redirectToRoute(
172
                'rfxAnalysisProject',
173
                [
174
                    'project' => $projectData->getDatabaseName(),
175
                ]
176
            );
177
        }
178
179
        $end = $rfx->getEndDate();
180
181
        // replace this example code with whatever you need
182
        return $this->render(
183
            'rfxAnalysis/result.html.twig',
184
            [
185
                'xtTitle' => $user->getUsername(),
186
                'xtPage' => 'rfx',
187
                'project' => $projectData,
188
                'user' => $user,
189
                'page' => $page,
190
                'type' => $type,
191
                'support' => $support,
192
                'oppose' => $oppose,
193
                'neutral' => $neutral,
194
                'total' => $total,
195
                'duplicates' => $dup,
196
                'enddate' => $end,
197
            ]
198
        );
199
    }
200
}
201