Passed
Push — master ( e6ce91...429907 )
by MusikAnimal
04:46
created

SimpleEditCounterController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the SimpleEditCounterController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Xtools\SimpleEditCounter;
14
use Xtools\SimpleEditCounterRepository;
15
16
/**
17
 * This controller handles the Simple Edit Counter tool.
18
 */
19
class SimpleEditCounterController extends XtoolsController
20
{
21
22
    /**
23
     * Get the name of the tool's index route.
24
     * This is also the name of the associated model.
25
     * @return string
26
     * @codeCoverageIgnore
27
     */
28
    public function getIndexRoute()
29
    {
30
        return 'SimpleEditCounter';
31
    }
32
33
    /**
34
     * SimpleEditCounterController constructor.
35
     * @param RequestStack $requestStack
36
     * @param ContainerInterface $container
37
     */
38 2
    public function __construct(RequestStack $requestStack, ContainerInterface $container)
39
    {
40 2
        $this->tooHighEditCountAction = $this->getIndexRoute();
41 2
        $this->tooHighEditCountActionBlacklist = ['index', 'result'];
42
43 2
        parent::__construct($requestStack, $container);
44 2
    }
45
46
    /**
47
     * The Simple Edit Counter search form.
48
     * @Route("/sc", name="SimpleEditCounter")
49
     * @Route("/sc/", name="SimpleEditCounterSlash")
50
     * @Route("/sc/index.php", name="SimpleEditCounterIndexPhp")
51
     * @Route("/sc/{project}", name="SimpleEditCounterProject")
52
     * @return Response
53
     */
54 1
    public function indexAction()
55
    {
56
        // Redirect if project and username are given.
57 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
58
            return $this->redirectToRoute('SimpleEditCounterResult', $this->params);
59
        }
60
61
        // Show the form.
62 1
        return $this->render('simpleEditCounter/index.html.twig', array_merge([
63 1
            'xtPageTitle' => 'tool-simpleeditcounter',
64 1
            'xtSubtitle' => 'tool-simpleeditcounter-desc',
65 1
            'xtPage' => 'simpleeditcounter',
66 1
            'project' => $this->project,
67
68
            // Defaults that will get overriden if in $params.
69 1
            'namespace' => 'all',
70 1
            'start' => '',
71 1
            'end' => '',
72 1
        ], $this->params));
73
    }
74
75
    /**
76
     * Display the
77
     * @Route(
78
     *     "/sc/{project}/{username}/{namespace}/{start}/{end}",
79
     *     name="SimpleEditCounterResult",
80
     *     requirements={
81
     *         "start" = "|\d{4}-\d{2}-\d{2}",
82
     *         "end" = "|\d{4}-\d{2}-\d{2}",
83
     *         "namespace" = "|all|\d+",
84
     *     },
85
     *     defaults={
86
     *         "start"=false,
87
     *         "end"=false,
88
     *     }
89
     * )
90
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
91
     * @return Response
92
     * @codeCoverageIgnore
93
     */
94
    public function resultAction($namespace = 'all')
95
    {
96
        $sec = new SimpleEditCounter(
97
            $this->container,
98
            $this->project,
99
            $this->user,
100
            $namespace,
101
            $this->start,
102
            $this->end
103
        );
104
        $secRepo = new SimpleEditCounterRepository();
105
        $secRepo->setContainer($this->container);
106
        $sec->setRepository($secRepo);
107
        $sec->prepareData();
108
109
        // Assign the values and display the template
110
        return $this->render('simpleEditCounter/result.html.twig', [
111
            'xtPage' => 'simpleeditcounter',
112
            'xtTitle' => $this->user->getUsername(),
113
            'user' => $this->user,
114
            'project' => $this->project,
115
            'sec' => $sec,
116
        ]);
117
    }
118
119
    /************************ API endpoints ************************/
120
121
    /**
122
     * API endpoint for the Simple Edit Counter.
123
     * @Route(
124
     *     "/api/user/simple_editcount/{project}/{username}/{namespace}/{start}/{end}",
125
     *     name="SimpleEditCounterApi",
126
     *     requirements={
127
     *         "username" = "(.+?)(?!(?:\/(\d+))?\/(?:\d{4}-\d{2}-\d{2})(?:\/(\d{4}-\d{2}-\d{2}))?)?$",
128
     *         "start" = "|\d{4}-\d{2}-\d{2}",
129
     *         "end" = "|\d{4}-\d{2}-\d{2}",
130
     *         "namespace" = "|all|\d+"
131
     *     },
132
     *     defaults={
133
     *         "start"=false,
134
     *         "end"=false,
135
     *     }
136
     * )
137
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
138
     * @return Response
139
     * @codeCoverageIgnore
140
     */
141
    public function simpleEditCounterApiAction($namespace = 'all')
142
    {
143
        $this->recordApiUsage('user/simple_editcount');
144
145
        $sec = new SimpleEditCounter(
146
            $this->container,
147
            $this->project,
148
            $this->user,
149
            $namespace,
150
            $this->start,
151
            $this->end
152
        );
153
        $secRepo = new SimpleEditCounterRepository();
154
        $secRepo->setContainer($this->container);
155
        $sec->setRepository($secRepo);
156
        $sec->prepareData();
157
158
        $ret = [
159
            'username' => $this->user->getUsername(),
160
        ];
161
162
        if ($namespace !== 'all') {
163
            $ret['namespace'] = $namespace;
164
        }
165
        if ($this->start !== false) {
166
            $ret['start'] = date('Y-m-d', $this->start);
0 ignored issues
show
Bug introduced by
It seems like $this->start can also be of type true; however, parameter $timestamp of date() does only seem to accept integer, 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

166
            $ret['start'] = date('Y-m-d', /** @scrutinizer ignore-type */ $this->start);
Loading history...
167
        }
168
        if ($this->end !== false) {
169
            $ret['end'] = date('Y-m-d', $this->end);
170
        }
171
172
        $ret = array_merge($ret, $sec->getData());
173
174
        $response = new JsonResponse();
175
        $response->setEncodingOptions(JSON_NUMERIC_CHECK);
176
        $response->setData($ret);
177
        return $response;
178
    }
179
}
180