Passed
Push — master ( 5f527e...bcf4fb )
by MusikAnimal
04:22
created

PagesController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 25 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
dl 28
loc 112
rs 10
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getToolShortname() 0 3 1
B resultAction() 26 26 2
A getSummaryColumns() 0 19 3
A indexAction() 0 22 3

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
 * 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
use Xtools\Pages;
17
18
/**
19
 * This controller serves the Pages tool.
20
 */
21
class PagesController extends XtoolsController
22
{
23
    const RESULTS_PER_PAGE = 1000;
24
25
    /**
26
     * Get the tool's shortname.
27
     * @return string
28
     * @codeCoverageIgnore
29
     */
30
    public function getToolShortname()
31
    {
32
        return 'pages';
33
    }
34
35
    /**
36
     * Display the form.
37
     * @Route("/pages", name="pages")
38
     * @Route("/pages", name="Pages")
39
     * @Route("/pages/", name="PagesSlash")
40
     * @Route("/pages/index.php", name="PagesIndexPhp")
41
     * @Route("/pages/{project}", name="PagesProject")
42
     * @param Request $request
43
     * @return Response
44
     */
45 1
    public function indexAction(Request $request)
46
    {
47 1
        $params = $this->parseQueryParams($request);
48
49
        // Redirect if at minimum project and username are given.
50 1
        if (isset($params['project']) && isset($params['username'])) {
51
            return $this->redirectToRoute('PagesResult', $params);
52
        }
53
54
        // Convert the given project (or default project) into a Project instance.
55 1
        $params['project'] = $this->getProjectFromQuery($params);
56
57
        // Otherwise fall through.
58 1
        return $this->render('pages/index.html.twig', array_merge([
59 1
            'xtPageTitle' => 'tool-pages',
60
            'xtSubtitle' => 'tool-pages-desc',
61
            'xtPage' => 'pages',
62
63
            // Defaults that will get overriden if in $params.
64
            'namespace' => 0,
65
            'redirects' => 'noredirects',
66 1
        ], $params));
67
    }
68
69
    /**
70
     * Display the results.
71
     * @Route("/pages/{project}/{username}/{namespace}/{redirects}/{offset}", name="PagesResult")
72
     * @param Request $request
73
     * @param string|int $namespace The ID of the namespace, or 'all' for all namespaces.
74
     * @param string $redirects Whether to follow redirects or not.
75
     * @param int $offset Which page of results to show, when the results are so large they are paginated.
76
     * @return RedirectResponse|Response
77
     * @codeCoverageIgnore
78
     */
79 View Code Duplication
    public function resultAction(Request $request, $namespace = '0', $redirects = 'noredirects', $offset = 0)
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...
80
    {
81
        $ret = $this->validateProjectAndUser($request, 'pages');
82
        if ($ret instanceof RedirectResponse) {
83
            return $ret;
84
        } else {
85
            list($projectData, $user) = $ret;
86
        }
87
88
        $pages = new Pages(
89
            $projectData,
90
            $user,
91
            $namespace,
92
            $redirects,
93
            $offset
94
        );
95
        $pages->prepareData();
96
97
        // Assign the values and display the template
98
        return $this->render('pages/result.html.twig', [
99
            'xtPage' => 'pages',
100
            'xtTitle' => $user->getUsername(),
101
            'project' => $projectData,
102
            'user' => $user,
103
            'summaryColumns' => $this->getSummaryColumns($redirects),
104
            'pages' => $pages,
105
        ]);
106
    }
107
108
    /**
109
     * What columns to show in namespace totals table.
110
     * @param  string $redirects One of 'noredirects', 'onlyredirects' or blank for both.
111
     * @return string[]
112
     * @codeCoverageIgnore
113
     */
114
    protected function getSummaryColumns($redirects)
115
    {
116
        $summaryColumns = ['namespace'];
117
        if ($redirects == 'onlyredirects') {
118
            // Don't show redundant pages column if only getting data on redirects.
119
            $summaryColumns[] = 'redirects';
120
        } elseif ($redirects == 'noredirects') {
121
            // Don't show redundant redirects column if only getting data on non-redirects.
122
            $summaryColumns[] = 'pages';
123
        } else {
124
            // Order is important here.
125
            $summaryColumns[] = 'pages';
126
            $summaryColumns[] = 'redirects';
127
        }
128
129
        // Always show deleted as the last column.
130
        $summaryColumns[] = 'deleted';
131
132
        return $summaryColumns;
133
    }
134
}
135