Passed
Push — master ( 9a481f...a0a42c )
by MusikAnimal
05:51
created

EditCounterController::namespaceTotalsAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 18
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the EditCounterController 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\Response;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Xtools\EditCounter;
14
use Xtools\EditCounterRepository;
15
use Xtools\Page;
16
use Xtools\Project;
17
use Xtools\ProjectRepository;
18
use Xtools\User;
19
20
/**
21
 * Class EditCounterController
22
 */
23
class EditCounterController extends XtoolsController
24
{
25
26
    /** @var User The user being queried. */
27
    protected $user;
28
29
    /** @var Project The project being queried. */
30
    protected $project;
31
32
    /** @var EditCounter The edit-counter, that does all the work. */
33
    protected $editCounter;
34
35
    /**
36
     * Get the tool's shortname.
37
     * @return string
38
     * @codeCoverageIgnore
39
     */
40
    public function getToolShortname()
41
    {
42
        return 'ec';
43
    }
44
45
    /**
46
     * Every action in this controller (other than 'index') calls this first.
47
     * If a response is returned, the calling action is expected to return it.
48
     * @param string $key API key, as given in the reuqest. Omit this for actions
49
     *   that are public (only /api/ec actions should pass this in).
50
     * @return null|RedirectResponse
51
     */
52
    protected function setUpEditCounter($key = null)
53
    {
54
        // Return the EditCounter if we already have one.
55
        if ($this->editCounter instanceof EditCounter) {
0 ignored issues
show
introduced by
The condition $this->editCounter instanceof Xtools\EditCounter can never be false since $this->editCounter is always a sub-type of Xtools\EditCounter.
Loading history...
56
            return null;
57
        }
58
59
        // Validate key if attempted to make internal API request.
60
        if ($key && (string)$key !== (string)$this->container->getParameter('secret')) {
61
            throw $this->createAccessDeniedException('This endpoint is for internal use only.');
62
        }
63
64
        // Will redirect to Simple Edit Counter if they have too many edits.
65
        $ret = $this->validateProjectAndUser('SimpleEditCounterResult');
66
        if ($ret instanceof RedirectResponse) {
67
            return $ret;
68
        } else {
69
            // Get Project and User instances.
70
            list($this->project, $this->user) = $ret;
71
        }
72
73
        // Instantiate EditCounter.
74
        $editCounterRepo = new EditCounterRepository();
75
        $editCounterRepo->setContainer($this->container);
76
        $this->editCounter = new EditCounter($this->project, $this->user);
77
        $this->editCounter->setRepository($editCounterRepo);
78
    }
79
80
    /**
81
     * The initial GET request that displays the search form.
82
     *
83
     * @Route("/ec", name="ec")
84
     * @Route("/ec", name="EditCounter")
85
     * @Route("/ec/", name="EditCounterSlash")
86
     * @Route("/ec/index.php", name="EditCounterIndexPhp")
87
     * @Route("/ec/{project}", name="EditCounterProject")
88
     * @return RedirectResponse|Response
89
     */
90 1
    public function indexAction()
91
    {
92 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
93
            return $this->redirectToRoute('EditCounterResult', $this->params);
94
        }
95
96
        // Convert the given project (or default project) into a Project instance.
97 1
        $this->params['project'] = $this->getProjectFromQuery($this->params);
98
99
        // Otherwise fall through.
100 1
        return $this->render('editCounter/index.html.twig', [
101 1
            'xtPageTitle' => 'tool-ec',
102 1
            'xtSubtitle' => 'tool-ec-desc',
103 1
            'xtPage' => 'ec',
104 1
            'project' => $this->params['project'],
105
        ]);
106
    }
107
108
    /**
109
     * Display all results.
110
     * @Route("/ec/{project}/{username}", name="EditCounterResult")
111
     * @return Response
112
     * @codeCoverageIgnore
113
     */
114
    public function resultAction()
115
    {
116
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
117
        if ($ret instanceof RedirectResponse) {
118
            return $ret;
119
        }
120
121
        // Asynchronously collect some of the data that will be shown.
122
        // If multithreading is turned off, the normal getters in the views will
123
        // collect the necessary data synchronously.
124
        if ($this->container->getParameter('app.multithread')) {
125
            $this->editCounter->prepareData($this->container);
0 ignored issues
show
Unused Code introduced by
The call to Xtools\EditCounter::prepareData() has too many arguments starting with $this->container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

125
            $this->editCounter->/** @scrutinizer ignore-call */ 
126
                                prepareData($this->container);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
126
        }
127
128
        $ret = [
129
            'xtTitle' => $this->user->getUsername() . ' - ' . $this->project->getTitle(),
130
            'xtPage' => 'ec',
131
            'user' => $this->user,
132
            'project' => $this->project,
133
            'ec' => $this->editCounter,
134
        ];
135
136
        // Used when querying for global rights changes.
137
        if ((bool)$this->container->hasParameter('app.is_labs')) {
138
            $ret['metaProject'] = ProjectRepository::getProject('metawiki', $this->container);
139
        }
140
141
        // Output the relevant format template.
142
        return $this->getFormattedReponse('editCounter/result', $ret);
143
    }
144
145
    /**
146
     * Display the general statistics section.
147
     * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats")
148
     * @return Response
149
     * @codeCoverageIgnore
150
     */
151
    public function generalStatsAction()
152
    {
153
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
154
        if ($ret instanceof RedirectResponse) {
155
            return $ret;
156
        }
157
158
        $ret = [
159
            'xtTitle' => $this->user->getUsername(),
160
            'xtPage' => 'ec',
161
            'is_sub_request' => $this->isSubRequest,
162
            'user' => $this->user,
163
            'project' => $this->project,
164
            'ec' => $this->editCounter,
165
        ];
166
167
        // Output the relevant format template.
168
        return $this->getFormattedReponse('editCounter/general_stats', $ret);
169
    }
170
171
    /**
172
     * Display the namespace totals section.
173
     * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals")
174
     * @return Response
175
     * @codeCoverageIgnore
176
     */
177
    public function namespaceTotalsAction()
178
    {
179
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
180
        if ($ret instanceof RedirectResponse) {
181
            return $ret;
182
        }
183
184
        $ret = [
185
            'xtTitle' => $this->user->getUsername(),
186
            'xtPage' => 'ec',
187
            'is_sub_request' => $this->isSubRequest,
188
            'user' => $this->user,
189
            'project' => $this->project,
190
            'ec' => $this->editCounter,
191
        ];
192
193
        // Output the relevant format template.
194
        return $this->getFormattedReponse('editCounter/namespace_totals', $ret);
195
    }
196
197
    /**
198
     * Display the timecard section.
199
     * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimecard")
200
     * @return Response
201
     * @codeCoverageIgnore
202
     */
203
    public function timecardAction()
204
    {
205
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
206
        if ($ret instanceof RedirectResponse) {
207
            return $ret;
208
        }
209
210
        $optedInPage = $this->project
211
            ->getRepository()
212
            ->getPage($this->project, $this->project->userOptInPage($this->user));
213
214
        $ret = [
215
            'xtTitle' => $this->user->getUsername(),
216
            'xtPage' => 'ec',
217
            'is_sub_request' => $this->isSubRequest,
218
            'user' => $this->user,
219
            'project' => $this->project,
220
            'ec' => $this->editCounter,
221
            'opted_in_page' => $optedInPage,
222
        ];
223
224
        // Output the relevant format template.
225
        return $this->getFormattedReponse('editCounter/timecard', $ret);
226
    }
227
228
    /**
229
     * Display the year counts section.
230
     * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts")
231
     * @return Response
232
     * @codeCoverageIgnore
233
     */
234
    public function yearcountsAction()
235
    {
236
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
237
        if ($ret instanceof RedirectResponse) {
238
            return $ret;
239
        }
240
241
        $ret = [
242
            'xtTitle' => $this->user->getUsername(),
243
            'xtPage' => 'ec',
244
            'is_sub_request' => $this->isSubRequest,
245
            'user' => $this->user,
246
            'project' => $this->project,
247
            'ec' => $this->editCounter,
248
        ];
249
250
        // Output the relevant format template.
251
        return $this->getFormattedReponse('editCounter/yearcounts', $ret);
252
    }
253
254
    /**
255
     * Display the month counts section.
256
     * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts")
257
     * @return Response
258
     * @codeCoverageIgnore
259
     */
260
    public function monthcountsAction()
261
    {
262
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
263
        if ($ret instanceof RedirectResponse) {
264
            return $ret;
265
        }
266
267
        $optedInPage = $this->project
268
            ->getRepository()
269
            ->getPage($this->project, $this->project->userOptInPage($this->user));
270
        $ret = [
271
            'xtTitle' => $this->user->getUsername(),
272
            'xtPage' => 'ec',
273
            'is_sub_request' => $this->isSubRequest,
274
            'user' => $this->user,
275
            'project' => $this->project,
276
            'ec' => $this->editCounter,
277
            'opted_in_page' => $optedInPage,
278
        ];
279
280
        // Output the relevant format template.
281
        return $this->getFormattedReponse('editCounter/monthcounts', $ret);
282
    }
283
284
    /**
285
     * Display the user rights changes section.
286
     * @Route("/ec-rightschanges/{project}/{username}", name="EditCounterRightsChanges")
287
     * @return Response
288
     * @codeCoverageIgnore
289
     */
290
    public function rightschangesAction()
291
    {
292
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
293
        if ($ret instanceof RedirectResponse) {
294
            return $ret;
295
        }
296
297
        $ret = [
298
            'xtTitle' => $this->user->getUsername(),
299
            'xtPage' => 'ec',
300
            'is_sub_request' => $this->isSubRequest,
301
            'user' => $this->user,
302
            'project' => $this->project,
303
            'ec' => $this->editCounter,
304
        ];
305
306
        if ((bool)$this->container->hasParameter('app.is_labs')) {
307
            $ret['metaProject'] = ProjectRepository::getProject('metawiki', $this->container);
308
        }
309
310
        // Output the relevant format template.
311
        return $this->getFormattedReponse('editCounter/rights_changes', $ret);
312
    }
313
314
    /**
315
     * Display the latest global edits section.
316
     * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal")
317
     * @return Response
318
     * @codeCoverageIgnore
319
     */
320
    public function latestglobalAction()
321
    {
322
        $ret = $this->setUpEditCounter();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter() targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
323
        if ($ret instanceof RedirectResponse) {
324
            return $ret;
325
        }
326
327
        return $this->render('editCounter/latest_global.html.twig', [
328
            'xtTitle' => $this->user->getUsername(),
329
            'xtPage' => 'ec',
330
            'is_sub_request' => $this->isSubRequest,
331
            'user' => $this->user,
332
            'project' => $this->project,
333
            'ec' => $this->editCounter,
334
        ]);
335
    }
336
337
338
    /**
339
     * Below are internal API endpoints for the Edit Counter.
340
     * All only respond with JSON and only to requests passing in the value
341
     * of the 'secret' parameter. This should not be used in JavaScript or clientside
342
     * applications, rather only used internally.
343
     */
344
345
    /**
346
     * Get (most) of the general statistics as JSON.
347
     * @Route("/api/ec/pairdata/{project}/{username}/{key}", name="EditCounterApiPairData")
348
     * @param string $key API key.
349
     * @return JsonResponse
350
     * @codeCoverageIgnore
351
     */
352
    public function pairDataApiAction($key)
353
    {
354
        $ret = $this->setUpEditCounter($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($key) targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
355
        if ($ret instanceof RedirectResponse) {
356
            return $ret;
357
        }
358
359
        return new JsonResponse(
360
            $this->editCounter->getPairData(),
361
            Response::HTTP_OK
362
        );
363
    }
364
365
    /**
366
     * Get various log counts for the user as JSON.
367
     * @Route("/api/ec/logcounts/{project}/{username}/{key}", name="EditCounterApiLogCounts")
368
     * @param string $key API key.
369
     * @return JsonResponse
370
     * @codeCoverageIgnore
371
     */
372
    public function logCountsApiAction($key)
373
    {
374
        $ret = $this->setUpEditCounter($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($key) targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
375
        if ($ret instanceof RedirectResponse) {
376
            return $ret;
377
        }
378
379
        return new JsonResponse(
380
            $this->editCounter->getLogCounts(),
381
            Response::HTTP_OK
382
        );
383
    }
384
385
    /**
386
     * Get edit sizes for the user as JSON.
387
     * @Route("/api/ec/editsizes/{project}/{username}/{key}", name="EditCounterApiEditSizes")
388
     * @param string $key API key.
389
     * @return JsonResponse
390
     * @codeCoverageIgnore
391
     */
392
    public function editSizesApiAction($key)
393
    {
394
        $ret = $this->setUpEditCounter($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($key) targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
395
        if ($ret instanceof RedirectResponse) {
396
            return $ret;
397
        }
398
399
        return new JsonResponse(
400
            $this->editCounter->getEditSizeData(),
401
            Response::HTTP_OK
402
        );
403
    }
404
405
    /**
406
     * Get the namespace totals for the user as JSON.
407
     * @Route("/api/ec/namespacetotals/{project}/{username}/{key}", name="EditCounterApiNamespaceTotals")
408
     * @param string $key API key.
409
     * @return Response
410
     * @codeCoverageIgnore
411
     */
412
    public function namespaceTotalsApiAction($key)
413
    {
414
        $ret = $this->setUpEditCounter($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($key) targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
415
        if ($ret instanceof RedirectResponse) {
416
            return $ret;
417
        }
418
419
        return new JsonResponse(
420
            $this->editCounter->namespaceTotals(),
421
            Response::HTTP_OK
422
        );
423
    }
424
425
    /**
426
     * Display or fetch the month counts for the user.
427
     * @Route("/api/ec/monthcounts/{project}/{username}/{key}", name="EditCounterApiMonthCounts")
428
     * @param string $key API key.
429
     * @return Response
430
     * @codeCoverageIgnore
431
     */
432
    public function monthcountsApiAction($key)
433
    {
434
        $ret = $this->setUpEditCounter($key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($key) targeting AppBundle\Controller\Edi...ler::setUpEditCounter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
435
        if ($ret instanceof RedirectResponse) {
436
            return $ret;
437
        }
438
439
        return new JsonResponse(
440
            $this->editCounter->monthCounts(),
441
            Response::HTTP_OK
442
        );
443
    }
444
}
445