Test Failed
Push — master ( e77fba...b33856 )
by MusikAnimal
07:16
created

TopEditsController::indexAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 3
nop 0
dl 0
loc 22
ccs 5
cts 8
cp 0.625
crap 4.8437
rs 9.7998
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the TopEditsController class.
4
 */
5
6
declare(strict_types=1);
7
8
namespace AppBundle\Controller;
9
10
use AppBundle\Helper\I18nHelper;
11
use AppBundle\Model\TopEdits;
12
use AppBundle\Repository\TopEditsRepository;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\RequestStack;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Routing\Annotation\Route;
18
19
/**
20
 * The Top Edits tool.
21
 */
22
class TopEditsController extends XtoolsController
23
{
24
    /**
25
     * Get the name of the tool's index route. This is also the name of the associated model.
26
     * @return string
27
     * @codeCoverageIgnore
28
     */
29
    public function getIndexRoute(): string
30
    {
31
        return 'TopEdits';
32
    }
33
34
    /**
35
     * TopEditsController constructor.
36
     * @param RequestStack $requestStack
37
     * @param ContainerInterface $container
38
     * @param I18nHelper $i18n
39
     */
40 1
    public function __construct(RequestStack $requestStack, ContainerInterface $container, I18nHelper $i18n)
41
    {
42 1
        $this->tooHighEditCountAction = $this->getIndexRoute();
43
44
        // The Top Edits by page action is exempt from the edit count limitation.
45 1
        $this->tooHighEditCountActionBlacklist = ['singlePageTopEdits'];
46
47 1
        $this->restrictedActions = ['namespaceTopEditsUserApi'];
48
49 1
        parent::__construct($requestStack, $container, $i18n);
50 1
    }
51
52
    /**
53
     * Display the form.
54
     * @Route("/topedits", name="topedits")
55
     * @Route("/topedits", name="TopEdits")
56
     * @Route("/topedits/index.php", name="TopEditsIndex")
57
     * @Route("/topedits/{project}", name="TopEditsProject")
58
     * @return Response
59
     */
60 1
    public function indexAction(): Response
61
    {
62
        // Redirect if at minimum project and username are provided.
63 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
64
            if (empty($this->params['page'])) {
65
                return $this->redirectToRoute('TopEditsResultNamespace', $this->params);
66
            }
67
            return $this->redirectToRoute('TopEditsResultPage', $this->params);
68
        }
69
70 1
        return $this->render('topedits/index.html.twig', array_merge([
71 1
            'xtPageTitle' => 'tool-topedits',
72
            'xtSubtitle' => 'tool-topedits-desc',
73
            'xtPage' => 'TopEdits',
74
75
            // Defaults that will get overriden if in $params.
76
            'namespace' => 0,
77
            'page' => '',
78
            'username' => '',
79
            'start' => '',
80
            'end' => '',
81 1
        ], $this->params, ['project' => $this->project]));
82
    }
83
84
    /**
85
     * Every action in this controller (other than 'index') calls this first.
86
     * @return TopEdits
87
     * @codeCoverageIgnore
88
     */
89
    public function setUpTopEdits(): TopEdits
90
    {
91
        $topEdits = new TopEdits(
92
            $this->project,
93
            $this->user,
94
            $this->page,
95
            $this->namespace,
96
            $this->start,
97
            $this->end,
98
            $this->limit,
99
            $this->offset
100
        );
101
102
        $topEditsRepo = new TopEditsRepository();
103
        $topEditsRepo->setContainer($this->container);
104
        $topEdits->setRepository($topEditsRepo);
105
106
        return $topEdits;
107
    }
108
109
    /**
110
     * List top edits by this user for all pages in a particular namespace.
111
     * @Route("/topedits/{project}/{username}/{namespace}/{start}/{end}",
112
     *     name="TopEditsResultNamespace",
113
     *     requirements={
114
     *         "namespace"="|all|\d+",
115
     *         "start"="|\d{4}-\d{2}-\d{2}",
116
     *         "end"="|\d{4}-\d{2}-\d{2}",
117
     *     },
118
     *     defaults={"namespace" = "all", "start"=false, "end"=false}
119
     * )
120
     * @return Response
121
     * @codeCoverageIgnore
122
     */
123
    public function namespaceTopEditsAction(): Response
124
    {
125
        /**
126
         * Max number of rows per namespace to show. `null` here will use the TopEdits default.
127
         * @var int
128
         */
129
        $this->limit = $this->isSubRequest ? 10 : $this->limit;
130
131
        $topEdits = $this->setUpTopEdits();
132
        $topEdits->prepareData();
133
134
        $ret = [
135
            'xtPage' => 'TopEdits',
136
            'xtTitle' => $this->user->getUsername(),
137
            'te' => $topEdits,
138
            'is_sub_request' => $this->isSubRequest,
139
        ];
140
141
        // Output the relevant format template.
142
        return $this->getFormattedResponse('topedits/result_namespace', $ret);
143
    }
144
145
    /**
146
     * List top edits by this user for a particular page.
147
     * @Route("/topedits/{project}/{username}/{namespace}/{page}/{start}/{end}",
148
     *     name="TopEditsResultPage",
149
     *     requirements={
150
     *         "namespace"="|all|\d+",
151
     *         "page"="(.+?)(?!\/(?:|\d{4}-\d{2}-\d{2})(?:\/(|\d{4}-\d{2}-\d{2}))?)?$",
152
     *         "start"="|\d{4}-\d{2}-\d{2}",
153
     *         "end"="|\d{4}-\d{2}-\d{2}",
154
     *     },
155
     *     defaults={"namespace"="all", "start"=false, "end"=false}
156
     * )
157
     * @todo Add pagination.
158
     * @return Response
159
     * @codeCoverageIgnore
160
     */
161
    public function singlePageTopEditsAction(): Response
162
    {
163
        $topEdits = $this->setUpTopEdits();
164
        $topEdits->prepareData();
165
166
        // Send all to the template.
167
        return $this->getFormattedResponse('topedits/result_article', [
168
            'xtPage' => 'TopEdits',
169
            'xtTitle' => $this->user->getUsername() . ' - ' . $this->page->getTitle(),
170
            'te' => $topEdits,
171
        ]);
172
    }
173
174
    /************************ API endpoints ************************/
175
176
    /**
177
     * List top edits by this user for all pages in a particular namespace.
178
     * @Route("/api/user/top_edits/{project}/{username}/{namespace}/{start}/{end}",
179
     *     name="UserApiTopEditsNamespace",
180
     *     requirements={
181
     *         "namespace"="|\d+|all",
182
     *         "start"="|\d{4}-\d{2}-\d{2}",
183
     *         "end"="|\d{4}-\d{2}-\d{2}",
184
     *     },
185
     *     defaults={"namespace"="all", "start"=false, "end"=false}
186
     * )
187
     * @return JsonResponse
188
     * @codeCoverageIgnore
189
     */
190
    public function namespaceTopEditsUserApiAction(): JsonResponse
191
    {
192
        $this->recordApiUsage('user/topedits');
193
194
        $topEdits = $this->setUpTopEdits();
195
        $topEdits->prepareData();
196
197
        return $this->getFormattedApiResponse([
198
            'top_edits' => $topEdits->getTopEdits(),
199
        ]);
200
    }
201
202
    /**
203
     * Get the all edits of a user to a specific page, maximum 1000.
204
     * @Route("/api/user/top_edits/{project}/{username}/{namespace}/{page}/{start}/{end}",
205
     *     name="UserApiTopEditsPage",
206
     *     requirements = {
207
     *         "namespace"="|\d+|all",
208
     *         "page"="(.+?)(?!\/(?:|\d{4}-\d{2}-\d{2})(?:\/(|\d{4}-\d{2}-\d{2}))?)?$",
209
     *         "start"="|\d{4}-\d{2}-\d{2}",
210
     *         "end"="|\d{4}-\d{2}-\d{2}",
211
     *     },
212
     *     defaults={"namespace"="all", "start"=false, "end"=false}
213
     * )
214
     * @todo Add pagination.
215
     * @return JsonResponse
216
     * @codeCoverageIgnore
217
     */
218
    public function singlePageTopEditsUserApiAction(): JsonResponse
219
    {
220
        $this->recordApiUsage('user/topedits');
221
222
        $topEdits = $this->setUpTopEdits();
223
        $topEdits->prepareData(false);
224
225
        return $this->getFormattedApiResponse([
226
            'top_edits' => $topEdits->getTopEdits(),
227
        ]);
228
    }
229
}
230