Passed
Push — master ( 0c65d8...e43fc5 )
by MusikAnimal
18:43
created

GlobalContribsController::resultsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 19
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace AppBundle\Controller;
5
6
use AppBundle\Model\GlobalContribs;
7
use AppBundle\Repository\GlobalContribsRepository;
8
use AppBundle\Repository\ProjectRepository;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
/**
13
 * This controller serves the search form and results for the Global Contributions tool.
14
 * @codeCoverageIgnore
15
 */
16
class GlobalContribsController extends XtoolsController
17
{
18
    /**
19
     * Get the name of the tool's index route. This is also the name of the associated model.
20
     * @return string
21
     * @codeCoverageIgnore
22
     */
23
    public function getIndexRoute(): string
24
    {
25
        return 'GlobalContribs';
26
    }
27
28
    /**
29
     * The search form.
30
     * @Route("/globalcontribs", name="GlobalContribs")
31
     * @Route("/ec-latestglobal", name="EditCounterLatestGlobalIndex")
32
     * @Route("/ec-latestglobal-contributions", name="EditCounterLatestGlobalContribsIndex")
33
     * @Route("/ec-latestglobaledits", name="EditCounterLatestGlobalEditsIndex")
34
     * @return Response
35
     */
36
    public function indexAction(): Response
37
    {
38
        // Redirect if username is given.
39
        if (isset($this->params['username'])) {
40
            return $this->redirectToRoute('GlobalContribsResult', $this->params);
41
        }
42
43
        // FIXME: Nasty hack until T226072 is resolved.
44
        $project = ProjectRepository::getProject($this->i18n->getLang().'.wikipedia', $this->container);
45
        if (!$project->exists()) {
46
            $project = ProjectRepository::getProject(
47
                $this->container->getParameter('central_auth_project'),
48
                $this->container
49
            );
50
        }
51
52
        return $this->render('globalContribs/index.html.twig', array_merge([
53
            'xtPage' => 'GlobalContribs',
54
            'xtPageTitle' => 'tool-globalcontribs',
55
            'xtSubtitle' => 'tool-globalcontribs-desc',
56
            'project' => $project,
57
58
            // Defaults that will get overridden if in $this->params.
59
            'namespace' => 'all',
60
            'start' => '',
61
            'end' => '',
62
        ], $this->params));
63
    }
64
65
    /**
66
     * Display the latest global edits tool. First two routes are legacy.
67
     * @Route(
68
     *     "/ec-latestglobal-contributions/{project}/{username}/{offset}",
69
     *     name="EditCounterLatestGlobalContribs",
70
     *     requirements={"offset" = "|\d*"},
71
     *     defaults={"offset" = 0}
72
     * )
73
     * @Route(
74
     *     "/ec-latestglobal/{project}/{username}/{offset}",
75
     *     name="EditCounterLatestGlobal",
76
     *     requirements={"offset" = "|\d*"},
77
     *     defaults={"offset" = 0}
78
     * ),
79
     * @Route(
80
     *     "/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
81
     *     name="GlobalContribsResult",
82
     *     requirements={
83
     *         "namespace" = "|all|\d+",
84
     *         "start" = "|\d*|\d{4}-\d{2}-\d{2}",
85
     *         "end" = "|\d{4}-\d{2}-\d{2}",
86
     *         "offset" = "|\d*",
87
     *     },
88
     *     defaults={
89
     *         "namespace"="all",
90
     *         "start"=false,
91
     *         "end"=false,
92
     *         "offset" = 0,
93
     *     }
94
     * ),
95
     * @return Response
96
     * @codeCoverageIgnore
97
     */
98
    public function resultsAction(): Response
99
    {
100
        $globalContribsRepo = new GlobalContribsRepository();
101
        $globalContribsRepo->setContainer($this->container);
102
        $globalContribs = new GlobalContribs($this->user, $this->namespace, $this->start, $this->end, $this->offset);
103
        $globalContribs->setRepository($globalContribsRepo);
104
        $defaultProject = ProjectRepository::getProject(
105
            $this->container->getParameter('central_auth_project'),
106
            $this->container
107
        );
108
        $defaultProject->getRepository()->setContainer($this->container);
109
110
        return $this->render('globalContribs/result.html.twig', [
111
            'xtTitle' => $this->user->getUsername(),
112
            'xtPage' => 'GlobalContribs',
113
            'is_sub_request' => $this->isSubRequest,
114
            'user' => $this->user,
115
            'project' => $defaultProject,
116
            'gc' => $globalContribs,
117
        ]);
118
    }
119
}
120