Completed
Push — master ( 9fe0d2...0e0f1f )
by Sam
03:08
created

SimpleEditCounterController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 8
dl 0
loc 168
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getToolShortname() 0 4 1
B indexAction() 0 25 5
D resultAction() 0 111 14
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 Xtools\Project;
14
use Xtools\ProjectRepository;
15
use Xtools\User;
16
17
/**
18
 * This controller handles the Simple Edit Counter tool.
19
 */
20
class SimpleEditCounterController extends Controller
21
{
22
23
    /**
24
     * Get the tool's shortname.
25
     * @return string
26
     */
27
    public function getToolShortname()
28
    {
29
        return 'sc';
30
    }
31
32
    /**
33
     * The Simple Edit Counter search form.
34
     * @Route("/sc", name="sc")
35
     * @Route("/sc", name="SimpleEditCounter")
36
     * @Route("/sc/", name="SimpleEditCounterSlash")
37
     * @Route("/sc/index.php", name="SimpleEditCounterIndexPhp")
38
     * @Route("/sc/{project}", name="SimpleEditCounterProject")
39
     * @param Request $request The HTTP request.
40
     * @param string $project The project database name or domain.
41
     * @return Response
42
     */
43
    public function indexAction(Request $request, $project = null)
44
    {
45
        // Get the query parameters.
46
        $projectName = $project ?: $request->query->get('project');
47
        $username = $request->query->get('username', $request->query->get('user'));
48
49
        // If we've got a project and user, redirect to results.
50
        if ($projectName != '' && $username != '') {
51
            $routeParams = [ 'project' => $projectName, 'username' => $username ];
52
            return $this->redirectToRoute('SimpleEditCounterResult', $routeParams);
53
        }
54
55
        // Instantiate the project if we can, or use the default.
56
        $theProject = (!empty($projectName))
57
            ? ProjectRepository::getProject($projectName, $this->container)
58
            : ProjectRepository::getDefaultProject($this->container);
59
60
        // Show the form.
61
        return $this->render('simpleEditCounter/index.html.twig', [
62
            'xtPageTitle' => 'tool-sc',
63
            'xtSubtitle' => 'tool-sc-desc',
64
            'xtPage' => 'sc',
65
            'project' => $theProject,
66
        ]);
67
    }
68
69
    /**
70
     * Display the
71
     * @Route("/sc/{project}/{username}", name="SimpleEditCounterResult")
72
     * @param string $project The project domain name.
73
     * @param string $username The username.
74
     * @return Response
75
     */
76
    public function resultAction($project, $username)
77
    {
78
        $lh = $this->get('app.labs_helper');
79
80
        /** @var Project $project */
81
        $project = ProjectRepository::getProject($project, $this->container);
0 ignored issues
show
Documentation introduced by
$project is of type object<Xtools\Project>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
83
        if (!$project->exists()) {
84
            $this->addFlash('notice', ['invalid-project', $project]);
0 ignored issues
show
Documentation introduced by
array('invalid-project', $project) is of type array<integer,string|obj...ect<Xtools\\Project>"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
            return $this->redirectToRoute('SimpleEditCounter');
86
        }
87
88
        $dbName = $project->getDatabaseName();
89
        $url = $project->getUrl();
90
91
        $userTable = $lh->getTable('user', $dbName);
92
        $archiveTable = $lh->getTable('archive', $dbName);
93
        $revisionTable = $lh->getTable('revision', $dbName);
94
        $userGroupsTable = $lh->getTable('user_groups', $dbName);
95
96
        /** @var Connection $conn */
97
        $conn = $this->get('doctrine')->getManager('replicas')->getConnection();
98
99
        // Prepare the query and execute
100
        $resultQuery = $conn->prepare("
101
            SELECT 'id' AS source, user_id as value FROM $userTable WHERE user_name = :username
102
            UNION
103
            SELECT 'arch' AS source, COUNT(*) AS value FROM $archiveTable WHERE ar_user_text = :username
104
            UNION
105
            SELECT 'rev' AS source, COUNT(*) AS value FROM $revisionTable WHERE rev_user_text = :username
106
            UNION
107
            SELECT 'groups' AS source, ug_group AS value
108
                FROM $userGroupsTable JOIN $userTable on user_id = ug_user WHERE user_name = :username
109
        ");
110
111
        $user = new User($username);
112
        $usernameParam = $user->getUsername();
113
        $resultQuery->bindParam('username', $usernameParam);
114
        $resultQuery->execute();
115
116
        if ($resultQuery->errorCode() > 0) {
117
            $this->addFlash('notice', [ 'no-result', $username ]);
0 ignored issues
show
Documentation introduced by
array('no-result', $username) is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
118
            return $this->redirectToRoute('SimpleEditCounterProject', [ 'project' => $project->getDomain() ]);
119
        }
120
121
        // Fetch the result data
122
        $results = $resultQuery->fetchAll();
123
124
        // Initialize the variables - just so we don't get variable undefined errors if there is a problem
125
        $id = '';
126
        $arch = '';
127
        $rev = '';
128
        $groups = '';
129
130
        // Iterate over the results, putting them in the right variables
131
        foreach ($results as $row) {
132
            if ($row['source'] == 'id') {
133
                $id = $row['value'];
134
            }
135
            if ($row['source'] == 'arch') {
136
                $arch = $row['value'];
137
            }
138
            if ($row['source'] == 'rev') {
139
                $rev = $row['value'];
140
            }
141
            if ($row['source'] == 'groups') {
142
                $groups .= $row['value']. ', ';
143
            }
144
        }
145
146
        // Unknown user - If the user is created the $results variable will have 3 entries.
147
        // This is a workaround to detect non-existent IPs.
148
        if (count($results) < 3 && $arch == 0 && $rev == 0) {
149
            $this->addFlash('notice', [ 'no-result', $username ]);
0 ignored issues
show
Documentation introduced by
array('no-result', $username) is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
151
            return $this->redirectToRoute('SimpleEditCounterProject', [ 'project' => $project->getDomain() ]);
152
        }
153
154
        // Remove the last comma and space
155
        if (strlen($groups) > 2) {
156
            $groups = substr($groups, 0, -2);
157
        }
158
159
        // If the user isn't in any groups, show a message.
160
        if (strlen($groups) == 0) {
161
            $groups = '---';
162
        }
163
164
        $globalGroups = '';
165
166
        if (boolval($this->getParameter('app.single_wiki'))) {
167
            // Retrieving the global groups, using the ApiHelper class
168
            $api = $this->get('app.api_helper');
169
            $globalGroups = $api->globalGroups($url, $username);
170
        }
171
172
        // Assign the values and display the template
173
        return $this->render('simpleEditCounter/result.html.twig', [
174
            'xtPage' => 'sc',
175
            'xtTitle' => $username,
176
            'user' => $user,
177
            'project' => $project,
178
            'project_url' => $url,
179
            'id' => $id,
180
            'arch' => $arch,
181
            'rev' => $rev + $arch,
182
            'live' => $rev,
183
            'groups' => $groups,
184
            'globalGroups' => $globalGroups,
185
        ]);
186
    }
187
}
188