Passed
Push — master ( 526c52...06ff74 )
by MusikAnimal
08:41 queued 03:03
created

EditCounterController::logCountsApiAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

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

128
    public function resultAction(Request $request, $project, /** @scrutinizer ignore-unused */ $username)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $project is not used and could be removed. ( Ignorable by Annotation )

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

128
    public function resultAction(Request $request, /** @scrutinizer ignore-unused */ $project, $username)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
    {
130
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
131
        if ($ret instanceof RedirectResponse) {
132
            return $ret;
133
        }
134
135
        $ret = [
136
            'xtTitle' => $this->user->getUsername() . ' - ' . $this->project->getTitle(),
137
            'xtPage' => 'ec',
138
            'user' => $this->user,
139
            'project' => $this->project,
140
            'ec' => $this->editCounter,
141
        ];
142
143
        // Used when querying for global rights changes.
144
        if ((bool)$this->container->hasParameter('app.is_labs')) {
145
            $ret['metaProject'] = ProjectRepository::getProject('metawiki', $this->container);
146
        }
147
148
        // Output the relevant format template.
149
        return $this->getFormattedReponse($request, 'editCounter/result', $ret);
150
    }
151
152
    /**
153
     * Display the general statistics section.
154
     * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats")
155
     * @param Request $request
156
     * @return Response
157
     * @codeCoverageIgnore
158
     */
159
    public function generalStatsAction(Request $request)
160
    {
161
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
162
        if ($ret instanceof RedirectResponse) {
163
            return $ret;
164
        }
165
166
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
167
        $ret = [
168
            'xtTitle' => $this->user->getUsername(),
169
            'xtPage' => 'ec',
170
            'is_sub_request' => $isSubRequest,
171
            'user' => $this->user,
172
            'project' => $this->project,
173
            'ec' => $this->editCounter,
174
        ];
175
176
        // Output the relevant format template.
177
        return $this->getFormattedReponse($request, 'editCounter/general_stats', $ret);
178
    }
179
180
    /**
181
     * Display the namespace totals section.
182
     * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals")
183
     * @param Request $request
184
     * @return Response
185
     * @codeCoverageIgnore
186
     */
187
    public function namespaceTotalsAction(Request $request)
188
    {
189
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
190
        if ($ret instanceof RedirectResponse) {
191
            return $ret;
192
        }
193
194
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
195
        $ret = [
196
            'xtTitle' => $this->user->getUsername(),
197
            'xtPage' => 'ec',
198
            'is_sub_request' => $isSubRequest,
199
            'user' => $this->user,
200
            'project' => $this->project,
201
            'ec' => $this->editCounter,
202
        ];
203
204
        // Output the relevant format template.
205
        return $this->getFormattedReponse($request, 'editCounter/namespace_totals', $ret);
206
    }
207
208
    /**
209
     * Display the timecard section.
210
     * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimecard")
211
     * @param Request $request
212
     * @return Response
213
     * @codeCoverageIgnore
214
     */
215
    public function timecardAction(Request $request)
216
    {
217
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
218
        if ($ret instanceof RedirectResponse) {
219
            return $ret;
220
        }
221
222
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
223
        $optedInPage = $this->project
224
            ->getRepository()
225
            ->getPage($this->project, $this->project->userOptInPage($this->user));
226
227
        $ret = [
228
            'xtTitle' => $this->user->getUsername(),
229
            'xtPage' => 'ec',
230
            'is_sub_request' => $isSubRequest,
231
            'user' => $this->user,
232
            'project' => $this->project,
233
            'ec' => $this->editCounter,
234
            'opted_in_page' => $optedInPage,
235
        ];
236
237
        // Output the relevant format template.
238
        return $this->getFormattedReponse($request, 'editCounter/timecard', $ret);
239
    }
240
241
    /**
242
     * Display the year counts section.
243
     * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts")
244
     * @param Request $request
245
     * @return Response
246
     * @codeCoverageIgnore
247
     */
248
    public function yearcountsAction(Request $request)
249
    {
250
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
251
        if ($ret instanceof RedirectResponse) {
252
            return $ret;
253
        }
254
255
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
256
        $ret = [
257
            'xtTitle' => $this->user->getUsername(),
258
            'xtPage' => 'ec',
259
            'is_sub_request' => $isSubRequest,
260
            'user' => $this->user,
261
            'project' => $this->project,
262
            'ec' => $this->editCounter,
263
        ];
264
265
        // Output the relevant format template.
266
        return $this->getFormattedReponse($request, 'editCounter/yearcounts', $ret);
267
    }
268
269
    /**
270
     * Display the month counts section.
271
     * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts")
272
     * @param Request $request
273
     * @return Response
274
     * @codeCoverageIgnore
275
     */
276
    public function monthcountsAction(Request $request)
277
    {
278
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
279
        if ($ret instanceof RedirectResponse) {
280
            return $ret;
281
        }
282
283
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
284
        $optedInPage = $this->project
285
            ->getRepository()
286
            ->getPage($this->project, $this->project->userOptInPage($this->user));
287
        $ret = [
288
            'xtTitle' => $this->user->getUsername(),
289
            'xtPage' => 'ec',
290
            'is_sub_request' => $isSubRequest,
291
            'user' => $this->user,
292
            'project' => $this->project,
293
            'ec' => $this->editCounter,
294
            'opted_in_page' => $optedInPage,
295
        ];
296
297
        // Output the relevant format template.
298
        return $this->getFormattedReponse($request, 'editCounter/monthcounts', $ret);
299
    }
300
301
    /**
302
     * Display the user rights changes section.
303
     * @Route("/ec-rightschanges/{project}/{username}", name="EditCounterRightsChanges")
304
     * @param Request $request
305
     * @return Response
306
     * @codeCoverageIgnore
307
     */
308
    public function rightschangesAction(Request $request)
309
    {
310
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
311
        if ($ret instanceof RedirectResponse) {
312
            return $ret;
313
        }
314
315
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
316
        $ret = [
317
            'xtTitle' => $this->user->getUsername(),
318
            'xtPage' => 'ec',
319
            'is_sub_request' => $isSubRequest,
320
            'user' => $this->user,
321
            'project' => $this->project,
322
            'ec' => $this->editCounter,
323
        ];
324
325
        if ((bool)$this->container->hasParameter('app.is_labs')) {
326
            $ret['metaProject'] = ProjectRepository::getProject('metawiki', $this->container);
327
        }
328
329
        // Output the relevant format template.
330
        return $this->getFormattedReponse($request, 'editCounter/rights_changes', $ret);
331
    }
332
333
    /**
334
     * Display the latest global edits section.
335
     * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal")
336
     * @param Request $request The HTTP request.
337
     * @return Response
338
     * @codeCoverageIgnore
339
     */
340
    public function latestglobalAction(Request $request)
341
    {
342
        $ret = $this->setUpEditCounter($request);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request) 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...
343
        if ($ret instanceof RedirectResponse) {
344
            return $ret;
345
        }
346
347
        $isSubRequest = $request->get('htmlonly')
348
                        || $this->container->get('request_stack')->getParentRequest() !== null;
349
        return $this->render('editCounter/latest_global.html.twig', [
350
            'xtTitle' => $this->user->getUsername(),
351
            'xtPage' => 'ec',
352
            'is_sub_request' => $isSubRequest,
353
            'user' => $this->user,
354
            'project' => $this->project,
355
            'ec' => $this->editCounter,
356
        ]);
357
    }
358
359
360
    /**
361
     * Below are internal API endpoints for the Edit Counter.
362
     * All only respond with JSON and only to requests passing in the value
363
     * of the 'secret' parameter. This should not be used in JavaScript or clientside
364
     * applications, rather only used internally.
365
     */
366
367
    /**
368
     * Get (most) of the general statistics as JSON.
369
     * @Route("/api/ec/pairdata/{project}/{username}/{key}", name="EditCounterApiPairData")
370
     * @param Request $request
371
     * @param string $key API key.
372
     * @return JsonResponse
373
     * @codeCoverageIgnore
374
     */
375
    public function pairDataApiAction(Request $request, $key)
376
    {
377
        $ret = $this->setUpEditCounter($request, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request, $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...
378
        if ($ret instanceof RedirectResponse) {
379
            return $ret;
380
        }
381
382
        return new JsonResponse(
383
            $this->editCounter->getPairData(),
384
            Response::HTTP_OK
385
        );
386
    }
387
388
    /**
389
     * Get various log counts for the user as JSON.
390
     * @Route("/api/ec/logcounts/{project}/{username}/{key}", name="EditCounterApiLogCounts")
391
     * @param Request $request
392
     * @param string $key API key.
393
     * @return JsonResponse
394
     * @codeCoverageIgnore
395
     */
396
    public function logCountsApiAction(Request $request, $key)
397
    {
398
        $ret = $this->setUpEditCounter($request, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request, $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...
399
        if ($ret instanceof RedirectResponse) {
400
            return $ret;
401
        }
402
403
        return new JsonResponse(
404
            $this->editCounter->getLogCounts(),
405
            Response::HTTP_OK
406
        );
407
    }
408
409
    /**
410
     * Get edit sizes for the user as JSON.
411
     * @Route("/api/ec/editsizes/{project}/{username}/{key}", name="EditCounterApiEditSizes")
412
     * @param Request $request
413
     * @param string $key API key.
414
     * @return JsonResponse
415
     * @codeCoverageIgnore
416
     */
417
    public function editSizesApiAction(Request $request, $key)
418
    {
419
        $ret = $this->setUpEditCounter($request, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request, $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...
420
        if ($ret instanceof RedirectResponse) {
421
            return $ret;
422
        }
423
424
        return new JsonResponse(
425
            $this->editCounter->getEditSizeData(),
426
            Response::HTTP_OK
427
        );
428
    }
429
430
    /**
431
     * Get the namespace totals for the user as JSON.
432
     * @Route("/api/ec/namespacetotals/{project}/{username}/{key}", name="EditCounterApiNamespaceTotals")
433
     * @param Request $request
434
     * @param string $key API key.
435
     * @return Response
436
     * @codeCoverageIgnore
437
     */
438
    public function namespaceTotalsApiAction(Request $request, $key)
439
    {
440
        $ret = $this->setUpEditCounter($request, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request, $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...
441
        if ($ret instanceof RedirectResponse) {
442
            return $ret;
443
        }
444
445
        return new JsonResponse(
446
            $this->editCounter->namespaceTotals(),
447
            Response::HTTP_OK
448
        );
449
    }
450
451
    /**
452
     * Display or fetch the month counts for the user.
453
     * @Route("/api/ec/monthcounts/{project}/{username}/{key}", name="EditCounterApiMonthCounts")
454
     * @param Request $request
455
     * @param string $key API key.
456
     * @return Response
457
     * @codeCoverageIgnore
458
     */
459
    public function monthcountsApiAction(Request $request, $key)
460
    {
461
        $ret = $this->setUpEditCounter($request, $key);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->setUpEditCounter($request, $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...
462
        if ($ret instanceof RedirectResponse) {
463
            return $ret;
464
        }
465
466
        return new JsonResponse(
467
            $this->editCounter->monthCounts(),
468
            Response::HTTP_OK
469
        );
470
    }
471
}
472