Passed
Push — master ( 95203a...4fa206 )
by MusikAnimal
04:54
created

SimpleEditCounterController::indexAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 2
nop 1
dl 0
loc 23
ccs 12
cts 13
cp 0.9231
crap 3.004
rs 9.0856
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 Doctrine\DBAL\Connection;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Xtools\SimpleEditCounter;
16
use Xtools\SimpleEditCounterRepository;
17
18
/**
19
 * This controller handles the Simple Edit Counter tool.
20
 */
21
class SimpleEditCounterController extends XtoolsController
22
{
23
24
    /**
25
     * Get the tool's shortname.
26
     * @return string
27
     * @codeCoverageIgnore
28
     */
29
    public function getToolShortname()
30
    {
31
        return 'sc';
32
    }
33
34
    /**
35
     * The Simple Edit Counter search form.
36
     * @Route("/sc", name="sc")
37
     * @Route("/sc", name="SimpleEditCounter")
38
     * @Route("/sc/", name="SimpleEditCounterSlash")
39
     * @Route("/sc/index.php", name="SimpleEditCounterIndexPhp")
40
     * @Route("/sc/{project}", name="SimpleEditCounterProject")
41
     * @param Request $request The HTTP request.
42
     * @return Response
43
     */
44 1
    public function indexAction(Request $request)
45
    {
46 1
        $params = $this->parseQueryParams($request);
47
48
        // Redirect if project and username are given.
49 1
        if (isset($params['project']) && isset($params['username'])) {
50
            return $this->redirectToRoute('SimpleEditCounterResult', $params);
51
        }
52
53
        // Convert the given project (or default project) into a Project instance.
54 1
        $params['project'] = $this->getProjectFromQuery($params);
55
56
        // Show the form.
57 1
        return $this->render('simpleEditCounter/index.html.twig', [
58 1
            'xtPageTitle' => 'tool-sc',
59 1
            'xtSubtitle' => 'tool-sc-desc',
60 1
            'xtPage' => 'sc',
61 1
            'project' => $params['project'],
62
63
            // Defaults that will get overriden if in $params.
64 1
            'namespace' => 'all',
65 1
            'start' => '',
66 1
            'end' => '',
67
        ]);
68
    }
69
70
    /**
71
     * Display the
72
     * @Route(
73
     *     "/sc/{project}/{username}/{namespace}/{start}/{end}",
74
     *     name="SimpleEditCounterResult",
75
     *     requirements={
76
     *         "start" = "|\d{4}-\d{2}-\d{2}",
77
     *         "end" = "|\d{4}-\d{2}-\d{2}",
78
     *         "namespace" = "|all|\d+"
79
     *     }
80
     * )
81
     * @param Request $request The HTTP request.
82
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
83
     * @param null|string $start
84
     * @param null|string $end
85
     * @return Response
86
     * @codeCoverageIgnore
87
     */
88
    public function resultAction(Request $request, $namespace = 'all', $start = false, $end = false)
89
    {
90
        $ret = $this->validateProjectAndUser($request);
91
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
introduced by
The condition $ret instanceof Symfony\...dation\RedirectResponse can never be false since $ret is always a sub-type of Symfony\Component\HttpFoundation\RedirectResponse.
Loading history...
92
            return $ret;
93
        } else {
94
            list($project, $user) = $ret;
95
        }
96
97
        // 'false' means the dates are optional and returned as 'false' if empty.
98
        list($start, $end) = $this->getUTCFromDateParams($start, $end, false);
99
100
        $sec = new SimpleEditCounter($this->container, $project, $user, $namespace, $start, $end);
101
        $secRepo = new SimpleEditCounterRepository();
102
        $secRepo->setContainer($this->container);
103
        $sec->setRepository($secRepo);
104
        $sec->prepareData();
105
106
        // Assign the values and display the template
107
        return $this->render('simpleEditCounter/result.html.twig', [
108
            'xtPage' => 'sc',
109
            'xtTitle' => $user->getUsername(),
110
            'user' => $user,
111
            'project' => $project,
112
            'sec' => $sec,
113
        ]);
114
    }
115
116
    /************************ API endpoints ************************/
117
118
    /**
119
     * API endpoint for the Simple Edit Counter.
120
     * @Route(
121
     *     "/api/user/simple_editcount/{project}/{username}/{namespace}/{start}/{end}",
122
     *     name="SimpleEditCounterApi",
123
     *     requirements={
124
     *         "start" = "|\d{4}-\d{2}-\d{2}",
125
     *         "end" = "|\d{4}-\d{2}-\d{2}",
126
     *         "namespace" = "|all|\d+"
127
     *     }
128
     * )
129
     * @param Request $request
130
     * @param int|string $namespace Namespace ID or 'all' for all namespaces.
131
     * @param null|string $start
132
     * @param null|string $end
133
     * @return Response
134
     * @codeCoverageIgnore
135
     */
136
    public function simpleEditCounterApiAction(
137
        Request $request,
138
        $namespace = 'all',
139
        $start = false,
140
        $end = false
141
    ) {
142
        $this->recordApiUsage('user/simple_editcount');
143
144
        // Here we do want to impose the max edit count restriction. Even though the
145
        // query is very 'simple', it can still run too slow for an API.
146
        $ret = $this->validateProjectAndUser($request, 'sc');
147
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
introduced by
The condition $ret instanceof Symfony\...dation\RedirectResponse can never be false since $ret is always a sub-type of Symfony\Component\HttpFoundation\RedirectResponse.
Loading history...
148
            return $ret;
149
        } else {
150
            list($project, $user) = $ret;
151
        }
152
153
        // 'false' means the dates are optional and returned as 'false' if empty.
154
        list($start, $end) = $this->getUTCFromDateParams($start, $end, false);
155
156
        $sec = new SimpleEditCounter($this->container, $project, $user, $namespace, $start, $end);
157
        $secRepo = new SimpleEditCounterRepository();
158
        $secRepo->setContainer($this->container);
159
        $sec->setRepository($secRepo);
160
        $sec->prepareData();
161
162
        $ret = [
163
            'username' => $user->getUsername(),
164
        ];
165
166
        if ($namespace !== 'all') {
167
            $ret['namespace'] = $namespace;
168
        }
169
        if ($start !== false) {
170
            $ret['start'] = date('Y-m-d', $start);
171
        }
172
        if ($end !== false) {
173
            $ret['end'] = date('Y-m-d', $end);
174
        }
175
176
        $ret = array_merge($ret, $sec->getData());
177
178
        $response = new JsonResponse();
179
        $response->setEncodingOptions(JSON_NUMERIC_CHECK);
180
        $response->setData($ret);
181
        return $response;
182
    }
183
}
184