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

SimpleEditCounterController::resultAction()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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