Passed
Push — master ( 35c320...7deb12 )
by MusikAnimal
04:42
created

EditSummaryController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
dl 0
loc 120
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 21 3
A editSummariesApiAction() 0 22 2
B resultAction() 0 28 2
A getIndexRoute() 0 3 1
1
<?php
2
/**
3
 * This file contains only the SimpleEditCounterController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use AppBundle\Helper\I18nHelper;
9
use Doctrine\DBAL\Connection;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Xtools\EditSummary;
16
use Xtools\EditSummaryRepository;
17
18
/**
19
 * This controller handles the Simple Edit Counter tool.
20
 */
21
class EditSummaryController extends XtoolsController
22
{
23
    /**
24
     * Get the name of the tool's index route.
25
     * This is also the name of the associated model.
26
     * @return string
27
     * @codeCoverageIgnore
28
     */
29
    public function getIndexRoute()
30
    {
31
        return 'EditSummary';
32
    }
33
34
    /**
35
     * The Edit Summary search form.
36
     *
37
     * @param Request $request The HTTP request.
38
     *
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'])) {
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-editsummary',
61
            'xtSubtitle' => 'tool-editsummary-desc',
62
            'xtPage' => 'editsummary',
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
     * @Route("/editsummary/{project}/{username}/{namespace}", name="EditSummaryResult")
72
     * @param Request $request The HTTP request.
73
     * @param string $namespace Namespace ID or 'all' for all namespaces.
74
     * @return Response
75
     * @codeCoverageIgnore
76
     */
77
    public function resultAction(Request $request, $namespace = 0)
78
    {
79
        $ret = $this->validateProjectAndUser($request, 'es');
80
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
introduced by
$ret is always a sub-type of Symfony\Component\HttpFoundation\RedirectResponse.
Loading history...
81
            return $ret;
82
        } else {
83
            list($project, $user) = $ret;
84
        }
85
86
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
87
        $editSummary = new EditSummary($project, $user, $namespace, 150);
88
        $editSummaryRepo = new EditSummaryRepository();
89
        $editSummaryRepo->setContainer($this->container);
90
        $editSummary->setRepository($editSummaryRepo);
91
        $editSummary->setI18nHelper($this->container->get('app.i18n_helper'));
92
93
        $editSummary->prepareData();
94
95
        // Assign the values and display the template
96
        return $this->render(
97
            'editSummary/result.html.twig',
98
            [
99
                'xtPage' => 'editsummary',
100
                'xtTitle' => $user->getUsername(),
101
                'user' => $user,
102
                'project' => $project,
103
                'namespace' => $namespace,
104
                'es' => $editSummary,
105
            ]
106
        );
107
    }
108
109
    /************************ API endpoints ************************/
110
111
    /**
112
     * Get basic stats on the edit summary usage of a user.
113
     * @Route("/api/user/edit_summaries/{project}/{username}/{namespace}", name="UserApiEditSummaries")
114
     * @param Request $request The HTTP request.
115
     * @param string $namespace Namespace ID or 'all' for all namespaces.
116
     * @return Response
117
     * @codeCoverageIgnore
118
     */
119
    public function editSummariesApiAction(Request $request, $namespace = 0)
120
    {
121
        $this->recordApiUsage('user/edit_summaries');
122
123
        $ret = $this->validateProjectAndUser($request);
124
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
introduced by
$ret is always a sub-type of Symfony\Component\HttpFoundation\RedirectResponse.
Loading history...
125
            // FIXME: needs to render as JSON, fetching the message from the FlashBag.
126
            return $ret;
127
        } else {
128
            list($project, $user) = $ret;
129
        }
130
131
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
132
        $editSummary = new EditSummary($project, $user, $namespace, 150, $this->container);
133
        $editSummaryRepo = new EditSummaryRepository();
134
        $editSummaryRepo->setContainer($this->container);
135
        $editSummary->setRepository($editSummaryRepo);
136
        $editSummary->prepareData();
137
138
        return new JsonResponse(
139
            $editSummary->getData(),
140
            Response::HTTP_OK
141
        );
142
    }
143
}
144