Passed
Push — master ( 5e3306...f3413b )
by MusikAnimal
05:40
created

EditSummaryController::editSummariesApiAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
crap 6
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
    public function indexAction(Request $request)
47
    {
48
        $params = $this->parseQueryParams($request);
49
50
        // If we've got a project, user, and namespace, redirect to results.
51
        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
        $params['project'] = $this->getProjectFromQuery($params);
57
58
        // Show the form.
59
        return $this->render('editSummary/index.html.twig', array_merge([
60
            '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
        ], $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);
0 ignored issues
show
Bug introduced by
It seems like $user can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $user can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
129
        $editSummary->prepareData();
130
131
        return new JsonResponse(
132
            $editSummary->getData(),
133
            Response::HTTP_OK
134
        );
135
    }
136
}
137