Passed
Push — master ( 9b6f1b...510c7f )
by MusikAnimal
04:53
created

EditSummaryController::indexAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 1
dl 0
loc 21
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.3142
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\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\RedirectResponse;
14
use Xtools\EditSummary;
15
use Xtools\EditSummaryRepository;
16
17
/**
18
 * This controller handles the Simple Edit Counter tool.
19
 */
20
class EditSummaryController extends XtoolsController
21
{
22
    /**
23
     * Get the tool's shortname.
24
     * @return string
25
     * @codeCoverageIgnore
26
     */
27
    public function getToolShortname()
28
    {
29
        return 'es';
30
    }
31
32
    /**
33
     * The Edit Summary search form.
34
     *
35
     * @param Request $request The HTTP request.
36
     *
37
     * @Route("/editsummary",           name="es")
38
     * @Route("/editsummary",           name="EditSummary")
39
     * @Route("/editsummary/",          name="EditSummarySlash")
40
     * @Route("/editsummary/index.php", name="EditSummaryIndexPhp")
41
     * @Route("/editsummary/{project}", name="EditSummaryProject")
42
     *
43
     * @return Response
44
     */
45 1
    public function indexAction(Request $request)
46
    {
47 1
        $params = $this->parseQueryParams($request);
48
49
        // If we've got a project, user, and namespace, redirect to results.
50 1
        if (isset($params['project']) && isset($params['username'])) {
51
            return $this->redirectToRoute('EditSummaryResult', $params);
52
        }
53
54
        // Convert the given project (or default project) into a Project instance.
55 1
        $params['project'] = $this->getProjectFromQuery($params);
56
57
        // Show the form.
58 1
        return $this->render('editSummary/index.html.twig', array_merge([
59 1
            'xtPageTitle' => 'tool-es',
60
            'xtSubtitle' => 'tool-es-desc',
61
            'xtPage' => 'es',
62
63
            // Defaults that will get overriden if in $params.
64
            'namespace' => 0,
65 1
        ], $params));
66
    }
67
68
    /**
69
     * Display the Edit Summary results
70
     *
71
     * @param Request $request The HTTP request.
72
     * @param string $namespace Namespace ID or 'all' for all namespaces.
73
     *
74
     * @Route("/editsummary/{project}/{username}/{namespace}", name="EditSummaryResult")
75
     *
76
     * @return Response
77
     * @codeCoverageIgnore
78
     */
79
    public function resultAction(Request $request, $namespace = 0)
80
    {
81
        $ret = $this->validateProjectAndUser($request, 'es');
82
        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...
83
            return $ret;
84
        } else {
85
            list($project, $user) = $ret;
86
        }
87
88
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
89
        $editSummary = new EditSummary($project, $user, $namespace, 150);
90
        $editSummaryRepo = new EditSummaryRepository();
91
        $editSummaryRepo->setContainer($this->container);
92
        $editSummary->setRepository($editSummaryRepo);
93
94
        $editSummary->prepareData();
95
96
        // Assign the values and display the template
97
        return $this->render(
98
            'editSummary/result.html.twig',
99
            [
100
                'xtPage' => 'es',
101
                'xtTitle' => $user->getUsername(),
102
                'user' => $user,
103
                'project' => $project,
104
                'namespace' => $namespace,
105
                'es' => $editSummary,
106
            ]
107
        );
108
    }
109
110
    /************************ API endpoints ************************/
111
112
    /**
113
     * Get basic stats on the edit summary usage of a user.
114
     * @Route("/api/user/edit_summaries/{project}/{username}/{namespace}", name="UserApiEditSummaries")
115
     * @param Request $request The HTTP request.
116
     * @param string $namespace Namespace ID or 'all' for all namespaces.
117
     * @return Response
118
     * @codeCoverageIgnore
119
     */
120
    public function editSummariesApiAction(Request $request, $namespace = 0)
121
    {
122
        $this->recordApiUsage('user/edit_summaries');
123
124
        $ret = $this->validateProjectAndUser($request);
125
        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...
126
            // FIXME: needs to render as JSON, fetching the message from the FlashBag.
127
            return $ret;
128
        } else {
129
            list($project, $user) = $ret;
130
        }
131
132
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
133
        $editSummary = new EditSummary($project, $user, $namespace, 150, $this->container);
134
        $editSummary->prepareData();
135
136
        return new JsonResponse(
137
            $editSummary->getData(),
138
            Response::HTTP_OK
139
        );
140
    }
141
}
142