Passed
Push — master ( e6ce91...429907 )
by MusikAnimal
04:46
created

EditSummaryController::indexAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 17
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.9666
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\Response;
11
use Xtools\EditSummary;
12
use Xtools\EditSummaryRepository;
13
14
/**
15
 * This controller handles the Simple Edit Counter tool.
16
 */
17
class EditSummaryController extends XtoolsController
18
{
19
    /**
20
     * Get the name of the tool's index route.
21
     * This is also the name of the associated model.
22
     * @return string
23
     * @codeCoverageIgnore
24
     */
25
    public function getIndexRoute()
26
    {
27
        return 'EditSummary';
28
    }
29
30
    /**
31
     * The Edit Summary search form.
32
     * @Route("/editsummary", name="EditSummary")
33
     * @Route("/editsummary/", name="EditSummarySlash")
34
     * @Route("/editsummary/index.php", name="EditSummaryIndexPhp")
35
     * @Route("/editsummary/{project}", name="EditSummaryProject")
36
     * @return Response
37
     */
38 1
    public function indexAction()
39
    {
40
        // If we've got a project, user, and namespace, redirect to results.
41 1
        if (isset($this->params['project']) && isset($this->params['username'])) {
42
            return $this->redirectToRoute('EditSummaryResult', $this->params);
43
        }
44
45
        // Show the form.
46 1
        return $this->render('editSummary/index.html.twig', array_merge([
47 1
            'xtPageTitle' => 'tool-editsummary',
48
            'xtSubtitle' => 'tool-editsummary-desc',
49
            'xtPage' => 'editsummary',
50
51
            // Defaults that will get overridden if in $params.
52
            'username' => '',
53
            'namespace' => 0,
54 1
        ], $this->params, ['project' => $this->project]));
55
    }
56
57
    /**
58
     * Display the Edit Summary results
59
     * @Route(
60
     *     "/editsummary/{project}/{username}/{namespace}", name="EditSummaryResult",
61
     *     requirements = {"namespace"="|\d+|all"},
62
     *     defaults={"namespace"=0}
63
     * )
64
     * @return Response
65
     * @codeCoverageIgnore
66
     */
67
    public function resultAction()
68
    {
69
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
70
        $editSummary = new EditSummary($this->project, $this->user, $this->namespace, 150);
71
        $editSummaryRepo = new EditSummaryRepository();
72
        $editSummaryRepo->setContainer($this->container);
73
        $editSummary->setRepository($editSummaryRepo);
74
        $editSummary->setI18nHelper($this->container->get('app.i18n_helper'));
75
        $editSummary->prepareData();
76
77
        // Assign the values and display the template
78
        return $this->render(
79
            'editSummary/result.html.twig',
80
            [
81
                'xtPage' => 'editsummary',
82
                'xtTitle' => $this->user->getUsername(),
83
                'user' => $this->user,
84
                'project' => $this->project,
85
                'namespace' => $this->namespace,
86
                'es' => $editSummary,
87
            ]
88
        );
89
    }
90
91
    /************************ API endpoints ************************/
92
93
    /**
94
     * Get basic stats on the edit summary usage of a user.
95
     * @Route(
96
     *     "/api/user/edit_summaries/{project}/{username}/{namespace}", name="UserApiEditSummaries",
97
     *     requirements = {"namespace"="|\d+|all"},
98
     *     defaults={"namespace"=0}
99
     * )
100
     * @return JsonResponse
101
     * @codeCoverageIgnore
102
     */
103
    public function editSummariesApiAction()
104
    {
105
        $this->recordApiUsage('user/edit_summaries');
106
107
        // Instantiate an EditSummary, treating the past 150 edits as 'recent'.
108
        $editSummary = new EditSummary($this->project, $this->user, $this->namespace, 150);
109
        $editSummaryRepo = new EditSummaryRepository();
110
        $editSummaryRepo->setContainer($this->container);
111
        $editSummary->setRepository($editSummaryRepo);
112
        $editSummary->setI18nHelper($this->container->get('app.i18n_helper'));
113
        $editSummary->prepareData();
114
115
        return $this->getFormattedApiResponse($editSummary->getData());
116
    }
117
}
118