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

EditSummaryController::getToolShortname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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