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

AutomatedEditsController::resultAction()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
rs 8.6961
c 1
b 0
f 0
cc 6
eloc 33
nc 17
nop 4

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 only the AutomatedEditsController class.
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\RedirectResponse;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Xtools\ProjectRepository;
14
use Xtools\User;
15
use Xtools\UserRepository;
16
17
/**
18
 * This controller serves the AutomatedEdits tool.
19
 */
20
class AutomatedEditsController extends XtoolsController
21
{
22
23
    /**
24
     * Get the tool's shortname.
25
     * @return string
26
     * @codeCoverageIgnore
27
     */
28
    public function getToolShortname()
29
    {
30
        return 'autoedits';
31
    }
32
33
    /**
34
     * Display the search form.
35
     * @Route("/autoedits", name="autoedits")
36
     * @Route("/autoedits/", name="autoeditsSlash")
37
     * @Route("/automatededits", name="autoeditsLong")
38
     * @Route("/automatededits/", name="autoeditsLongSlash")
39
     * @Route("/autoedits/index.php", name="autoeditsIndexPhp")
40
     * @Route("/automatededits/index.php", name="autoeditsLongIndexPhp")
41
     * @Route("/autoedits/{project}", name="autoeditsProject")
42
     * @param Request $request The HTTP request.
43
     * @return Response
44
     */
45 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...
46
    {
47
        $params = $this->parseQueryParams($request);
48
49
        // Redirect if at minimum project and username are provided.
50
        if (isset($params['project']) && isset($params['username'])) {
51
            return $this->redirectToRoute('autoeditsResult', $params);
52
        }
53
54
        // Convert the given project (or default project) into a Project instance.
55
        $params['project'] = $this->getProjectFromQuery($params);
56
57
        return $this->render('autoEdits/index.html.twig', array_merge([
58
            'xtPageTitle' => 'tool-autoedits',
59
            'xtSubtitle' => 'tool-autoedits-desc',
60
            'xtPage' => 'autoedits',
61
62
            // Defaults that will get overriden if in $params.
63
            'namespace' => 0,
64
            'start' => '',
65
            'end' => '',
66
        ], $params));
67
    }
68
69
    /**
70
     * Display the results.
71
     * @Route(
72
     *     "/autoedits/{project}/{username}/{namespace}/{start}/{end}", name="autoeditsResult",
73
     *     requirements={
74
     *         "start" = "|\d{4}-\d{2}-\d{2}",
75
     *         "end" = "|\d{4}-\d{2}-\d{2}",
76
     *         "namespace" = "|all|\d"
77
     *     }
78
     * )
79
     * @param Request $request The HTTP request.
80
     * @param int|string $namespace
81
     * @param null|string $start
82
     * @param null|string $end
83
     * @return RedirectResponse|Response
84
     * @codeCoverageIgnore
85
     */
86
    public function resultAction(Request $request, $namespace = 0, $start = null, $end = null)
87
    {
88
        // Will redirect back to index if the user has too high of an edit count.
89
        $ret = $this->validateProjectAndUser($request, 'autoedits');
90
        if ($ret instanceof RedirectResponse) {
91
            return $ret;
92
        } else {
93
            list($projectData, $user) = $ret;
94
        }
95
96
        // 'false' means the dates are optional and returned as 'false' if empty.
97
        list($start, $end) = $this->getUTCFromDateParams($start, $end, false);
98
99
        // We'll want to conditionally show some things in the view if there is a start date.
100
        $hasStartDate = $start > 0;
101
102
        // Format dates as needed by User model, if the date is present.
103
        if ($start !== false) {
104
            $start = date('Y-m-d', $start);
105
        }
106
        if ($end !== false) {
107
            $end = date('Y-m-d', $end);
108
        }
109
110
        // Normalize default namespace.
111
        if ($namespace == '') {
112
            $namespace = 0;
113
        }
114
115
        $editCount = $user->countEdits($projectData, $namespace, $start, $end);
116
117
        // Get individual counts of how many times each tool was used.
118
        // This also includes a wikilink to the tool.
119
        $toolCounts = $user->getAutomatedCounts($projectData, $namespace, $start, $end);
120
        $toolsTotal = array_reduce($toolCounts, function ($a, $b) {
121
            return $a + $b['count'];
122
        });
123
124
        // Query to get combined (semi)automated using for all edits
125
        //   as some automated edits overlap.
126
        $autoCount = $user->countAutomatedEdits($projectData, $namespace, $start, $end);
127
128
        $ret = [
129
            'xtPage' => 'autoedits',
130
            'user' => $user,
131
            'project' => $projectData,
132
            'toolCounts' => $toolCounts,
133
            'toolsTotal' => $toolsTotal,
134
            'autoCount' => $autoCount,
135
            'editCount' => $editCount,
136
            'autoPct' => $editCount ? ($autoCount / $editCount) * 100 : 0,
137
            'hasStartDate' => $hasStartDate,
138
            'start' => $start,
139
            'end' => $end,
140
            'namespace' => $namespace,
141
        ];
142
143
        // Render the view with all variables set.
144
        return $this->render('autoEdits/result.html.twig', $ret);
145
    }
146
}
147