Passed
Push — master ( 75dacc...adc049 )
by MusikAnimal
05:30
created

prepareSimpleEditCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 19
ccs 0
cts 14
cp 0
crap 6
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the SimpleEditCounterController class.
4
 */
5
6
declare(strict_types=1);
7
8
namespace AppBundle\Controller;
9
10
use AppBundle\Model\SimpleEditCounter;
11
use AppBundle\Repository\SimpleEditCounterRepository;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * This controller handles the Simple Edit Counter tool.
17
 */
18
class SimpleEditCounterController extends XtoolsController
19
{
20
21
    /**
22
     * Get the name of the tool's index route.
23
     * This is also the name of the associated model.
24
     * @return string
25
     * @codeCoverageIgnore
26
     */
27
    public function getIndexRoute(): string
28
    {
29
        return 'SimpleEditCounter';
30
    }
31
32
    /**
33
     * The Simple Edit Counter search form.
34
     * @Route("/sc", name="SimpleEditCounter")
35
     * @Route("/sc/index.php", name="SimpleEditCounterIndexPhp")
36
     * @Route("/sc/{project}", name="SimpleEditCounterProject")
37
     * @return Response
38
     */
39 1
    public function indexAction(): Response
40
    {
41
        // Redirect if project and username are given.
42 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
43
            return $this->redirectToRoute('SimpleEditCounterResult', $this->params);
44
        }
45
46
        // Show the form.
47 1
        return $this->render('simpleEditCounter/index.html.twig', array_merge([
48 1
            'xtPageTitle' => 'tool-simpleeditcounter',
49 1
            'xtSubtitle' => 'tool-simpleeditcounter-desc',
50 1
            'xtPage' => 'SimpleEditCounter',
51 1
            'project' => $this->project,
52
53
            // Defaults that will get overridden if in $params.
54 1
            'namespace' => 'all',
55 1
            'start' => '',
56 1
            'end' => '',
57 1
        ], $this->params, ['project' => $this->project]));
58
    }
59
60
    private function prepareSimpleEditCounter(): SimpleEditCounter
61
    {
62
        $sec = new SimpleEditCounter(
63
            $this->project,
64
            $this->user,
65
            $this->namespace,
66
            $this->start,
67
            $this->end
68
        );
69
        $secRepo = new SimpleEditCounterRepository();
70
        $secRepo->setContainer($this->container);
71
        $sec->setRepository($secRepo);
72
        $sec->prepareData();
73
74
        if ($sec->isLimited()) {
75
            $this->addFlash('warning', $this->i18n->msg('simple-counter-limited-results'));
0 ignored issues
show
Bug introduced by
It seems like $this->i18n->msg('simple...unter-limited-results') can also be of type null; however, parameter $message of Symfony\Bundle\Framework...\Controller::addFlash() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

75
            $this->addFlash('warning', /** @scrutinizer ignore-type */ $this->i18n->msg('simple-counter-limited-results'));
Loading history...
76
        }
77
78
        return $sec;
79
    }
80
81
    /**
82
     * Display the results.
83
     * @Route(
84
     *     "/sc/{project}/{username}/{namespace}/{start}/{end}",
85
     *     name="SimpleEditCounterResult",
86
     *     requirements={
87
     *         "start" = "|\d{4}-\d{2}-\d{2}",
88
     *         "end" = "|\d{4}-\d{2}-\d{2}",
89
     *         "namespace" = "|all|\d+",
90
     *     },
91
     *     defaults={
92
     *         "start"=false,
93
     *         "end"=false,
94
     *         "namespace"="all",
95
     *     }
96
     * )
97
     * @return Response
98
     * @codeCoverageIgnore
99
     */
100
    public function resultAction(): Response
101
    {
102
        $sec = $this->prepareSimpleEditCounter();
103
104
        return $this->getFormattedResponse('simpleEditCounter/result', [
105
            'xtPage' => 'SimpleEditCounter',
106
            'xtTitle' => $this->user->getUsername(),
107
            'sec' => $sec,
108
        ]);
109
    }
110
111
    /************************ API endpoints ************************/
112
113
    /**
114
     * API endpoint for the Simple Edit Counter.
115
     * @Route(
116
     *     "/api/user/simple_editcount/{project}/{username}/{namespace}/{start}/{end}",
117
     *     name="SimpleEditCounterApi",
118
     *     requirements={
119
     *         "username" = "(.+?)(?!(?:\/(\d+))?\/(?:\d{4}-\d{2}-\d{2})(?:\/(\d{4}-\d{2}-\d{2}))?)?$",
120
     *         "start" = "|\d{4}-\d{2}-\d{2}",
121
     *         "end" = "|\d{4}-\d{2}-\d{2}",
122
     *         "namespace" = "|all|\d+"
123
     *     },
124
     *     defaults={
125
     *         "start"=false,
126
     *         "end"=false,
127
     *         "namespace"="all",
128
     *     }
129
     * )
130
     * @return Response
131
     * @codeCoverageIgnore
132
     */
133
    public function simpleEditCounterApiAction(): Response
134
    {
135
        $sec = $this->prepareSimpleEditCounter();
136
        return $this->getFormattedApiResponse($sec->getData());
137
    }
138
}
139