Passed
Pull Request — master (#125)
by MusikAnimal
03:53
created

EditSummaryController::indexAction()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

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