Completed
Push — master ( e3e8c6...010637 )
by MusikAnimal
12s
created

PagesController::resultAction()   D

Complexity

Conditions 12
Paths 223

Size

Total Lines 112
Code Lines 74

Duplication

Lines 18
Ratio 16.07 %

Importance

Changes 0
Metric Value
dl 18
loc 112
rs 4.2804
c 0
b 0
f 0
cc 12
eloc 74
nc 223
nop 3

How to fix   Long Method    Complexity   

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 only the PagesController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use DateTime;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Xtools\ProjectRepository;
15
use Xtools\UserRepository;
16
17
/**
18
 * This controller serves the Pages tool.
19
 */
20
class PagesController extends XtoolsController
21
{
22
23
    /**
24
     * Get the tool's shortname.
25
     * @return string
26
     * @codeCoverageIgnore
27
     */
28
    public function getToolShortname()
29
    {
30
        return 'pages';
31
    }
32
33
    /**
34
     * Display the form.
35
     * @Route("/pages", name="pages")
36
     * @Route("/pages", name="Pages")
37
     * @Route("/pages/", name="PagesSlash")
38
     * @Route("/pages/index.php", name="PagesIndexPhp")
39
     * @Route("/pages/{project}", name="PagesProject")
40
     * @param Request $request
41
     * @return Response
42
     */
43 View Code Duplication
    public function indexAction(Request $request)
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...
44
    {
45
        $params = $this->parseQueryParams($request);
46
47
        // Redirect if at minimum project and username are given.
48
        if (isset($params['project']) && isset($params['username'])) {
49
            return $this->redirectToRoute('PagesResult', $params);
50
        }
51
52
        // Convert the given project (or default project) into a Project instance.
53
        $params['project'] = $this->getProjectFromQuery($params);
54
55
        // Otherwise fall through.
56
        return $this->render('pages/index.html.twig', array_merge([
57
            'xtPageTitle' => 'tool-pages',
58
            'xtSubtitle' => 'tool-pages-desc',
59
            'xtPage' => 'pages',
60
61
            // Defaults that will get overriden if in $params.
62
            'namespace' => 0,
63
            'redirects' => 'noredirects',
64
        ], $params));
65
    }
66
67
    /**
68
     * Display the results.
69
     * @Route("/pages/{project}/{username}/{namespace}/{redirects}", name="PagesResult")
70
     * @param Request $request
71
     * @param string $namespace The ID of the namespace.
72
     * @param string $redirects Whether to follow redirects or not.
73
     * @return RedirectResponse|Response
74
     * @codeCoverageIgnore
75
     */
76
    public function resultAction(Request $request, $namespace = '0', $redirects = 'noredirects')
77
    {
78
        $ret = $this->validateProjectAndUser($request, 'topedits');
79
        if ($ret instanceof RedirectResponse) {
80
            return $ret;
81
        } else {
82
            list($projectData, $user) = $ret;
83
        }
84
85
        // what columns to show in namespace totals table
86
        $summaryColumns = ['namespace'];
87
        if ($redirects == 'onlyredirects') {
88
            // don't show redundant pages column if only getting data on redirects
89
            $summaryColumns[] = 'redirects';
90
        } elseif ($redirects == 'noredirects') {
91
            // don't show redundant redirects column if only getting data on non-redirects
92
            $summaryColumns[] = 'pages';
93
        } else {
94
            // order is important here
95
            $summaryColumns[] = 'pages';
96
            $summaryColumns[] = 'redirects';
97
        }
98
        $summaryColumns[] = 'deleted'; // always show deleted column
99
100
        $result = $user->getRepository()->getPagesCreated($projectData, $user, $namespace, $redirects);
101
102
        $hasPageAssessments = $projectData->hasPageAssessments();
103
        $pagesByNamespaceByDate = [];
104
        $pageTitles = [];
105
        $countsByNamespace = [];
106
        $total = 0;
107
        $redirectTotal = 0;
108
        $deletedTotal = 0;
109
110
        foreach ($result as $row) {
111
            $datetime = DateTime::createFromFormat('YmdHis', $row['rev_timestamp']);
112
            $datetimeKey = $datetime->format('Ymdhi');
113
            $datetimeHuman = $datetime->format('Y-m-d H:i');
114
115
            $pageData = array_merge($row, [
116
                'raw_time' => $row['rev_timestamp'],
117
                'human_time' => $datetimeHuman,
118
                'page_title' => str_replace('_', ' ', $row['page_title'])
119
            ]);
120
121
            if ($hasPageAssessments) {
122
                $pageData['badge'] = $projectData->getAssessmentBadgeURL($pageData['pa_class']);
123
            }
124
125
            $pagesByNamespaceByDate[$row['namespace']][$datetimeKey][] = $pageData;
126
127
            $pageTitles[] = $row['page_title'];
128
129
            // Totals
130
            if (isset($countsByNamespace[$row['namespace']]['total'])) {
131
                $countsByNamespace[$row['namespace']]['total']++;
132
            } else {
133
                $countsByNamespace[$row['namespace']]['total'] = 1;
134
                $countsByNamespace[$row['namespace']]['redirect'] = 0;
135
                $countsByNamespace[$row['namespace']]['deleted'] = 0;
136
            }
137
            $total++;
138
139 View Code Duplication
            if ($row['page_is_redirect']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
140
                $redirectTotal++;
141
                // Redirects
142
                if (isset($countsByNamespace[$row['namespace']]['redirect'])) {
143
                    $countsByNamespace[$row['namespace']]['redirect']++;
144
                } else {
145
                    $countsByNamespace[$row['namespace']]['redirect'] = 1;
146
                }
147
            }
148
149 View Code Duplication
            if ($row['type'] === 'arc') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
150
                $deletedTotal++;
151
                // Deleted
152
                if (isset($countsByNamespace[$row['namespace']]['deleted'])) {
153
                    $countsByNamespace[$row['namespace']]['deleted']++;
154
                } else {
155
                    $countsByNamespace[$row['namespace']]['deleted'] = 1;
156
                }
157
            }
158
        }
159
160
        ksort($pagesByNamespaceByDate);
161
        ksort($countsByNamespace);
162
163
        foreach (array_keys($pagesByNamespaceByDate) as $key) {
164
            krsort($pagesByNamespaceByDate[$key]);
165
        }
166
167
        // Retrieve the namespaces
168
        $namespaces = $projectData->getNamespaces();
169
170
        // Assign the values and display the template
171
        return $this->render('pages/result.html.twig', [
172
            'xtPage' => 'pages',
173
            'xtTitle' => $user->getUsername(),
174
            'project' => $projectData,
175
            'user' => $user,
176
            'namespace' => $namespace,
177
            'redirect' => $redirects,
178
            'summaryColumns' => $summaryColumns,
179
            'namespaces' => $namespaces,
180
            'pages' => $pagesByNamespaceByDate,
181
            'count' => $countsByNamespace,
182
            'total' => $total,
183
            'redirectTotal' => $redirectTotal,
184
            'deletedTotal' => $deletedTotal,
185
            'hasPageAssessments' => $hasPageAssessments,
186
        ]);
187
    }
188
}
189