Completed
Push — master ( 1ced49...3e4594 )
by Sam
03:18
created

EditCounterController::setUpEditCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Helper\ApiHelper;
6
use AppBundle\Helper\AutomatedEditsHelper;
7
use AppBundle\Helper\LabsHelper;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\VarDumper\VarDumper;
12
use Xtools\EditCounter;
13
use Xtools\EditCounterRepository;
14
use Xtools\Project;
15
use Xtools\ProjectRepository;
16
use Xtools\User;
17
use Xtools\UserRepository;
18
19
class EditCounterController extends Controller
20
{
21
22
    /** @var User */
23
    protected $user;
24
    
25
    /** @var Project */
26
    protected $project;
27
    
28
    /** @var EditCounter */
29
    protected $editCounter;
30
31
    /**
32
     * Every action in this controller (other than 'index') calls this first.
33
     * @param string|boolean $project
34
     */
35
    protected function setUpEditCounter($project = false, $username = false)
36
    {
37
        // Make sure EditCounter is enabled.
38
        $this->get('app.labs_helper')->checkEnabled("ec");
39
40
        $this->project = ProjectRepository::getProject($project, $this->container);
1 ignored issue
show
Bug introduced by
It seems like $project defined by parameter $project on line 35 can also be of type boolean; however, Xtools\ProjectRepository::getProject() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
        $this->user = UserRepository::getUser($username, $this->container);
1 ignored issue
show
Documentation introduced by
$username is of type boolean, 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...
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
42
43
        // Get an edit-counter.
44
        $editCounterRepo = new EditCounterRepository();
45
        $editCounterRepo->setContainer($this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
46
        $this->editCounter = new EditCounter($this->project, $this->user);
47
        $this->editCounter->setRepository($editCounterRepo);
48
49
        // Don't continue if the user doesn't exist.
50
        if (!$this->user->existsOnProject($this->project)) {
51
            $this->container->getSession()->getFlashBag()->set('notice', 'user-not-found');
0 ignored issues
show
Bug introduced by
The method getSession() does not seem to exist on object<Symfony\Component...ion\ContainerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
        }
53
    }
54
55
    /**
56
     * @Route("/ec", name="ec")
57
     * @Route("/ec", name="EditCounter")
58
     * @Route("/ec/", name="EditCounterSlash")
59
     * @Route("/ec/index.php", name="EditCounterIndexPhp")
60
     * @Route("/ec/{project}", name="EditCounterProject")
61
     */
62
    public function indexAction(Request $request, $project = null)
63
    {
64
        $queryProject = $request->query->get('project');
65
        $username = $request->query->get('username');
66
67
        if (($project || $queryProject) && $username) {
68
            $routeParams = [ 'project'=>($project ?: $queryProject), 'username' => $username ];
69
            return $this->redirectToRoute("EditCounterResult", $routeParams);
70
        } elseif (!$project && $queryProject) {
71
            return $this->redirectToRoute("EditCounterProject", [ 'project'=>$queryProject ]);
72
        }
73
74
        $project = ProjectRepository::getProject($queryProject, $this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
        if (!$project->exists()) {
76
            $project = ProjectRepository::getDefaultProject($this->container);
1 ignored issue
show
Compatibility introduced by
$this->container of type object<Symfony\Component...ion\ContainerInterface> is not a sub-type of object<Symfony\Component...ncyInjection\Container>. It seems like you assume a concrete implementation of the interface Symfony\Component\Depend...tion\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
        }
78
79
        // Otherwise fall through.
80
        return $this->render('editCounter/index.html.twig', [
81
            "xtPageTitle" => "tool-ec",
82
            "xtSubtitle" => "tool-ec-desc",
83
            'xtPage' => "ec",
84
            'project' => $project,
85
        ]);
86
    }
87
88
    /**
89
     * @Route("/ec/{project}/{username}", name="EditCounterResult")
90
     */
91
    public function resultAction(Request $request, $project, $username)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        $this->setUpEditCounter($project, $username);
94
95
        //$automatedEditsSummary = $automatedEditsHelper->getEditsSummary($user->getId());
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
97
        // Give it all to the template.
98
        return $this->render('editCounter/result.html.twig', [
99
            'xtTitle' => $username,
100
            'xtPage' => 'ec',
101
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
102
            'is_labs' => $this->project->getRepository()->isLabs(),
103
            'user' => $this->user,
104
            'project' => $this->project,
105
            'ec' => $this->editCounter,
106
107
            // Automated edits.
108
            //'auto_edits' => $automatedEditsSummary,
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
        ]);
110
    }
111
112
    /**
113
     * @Route("/ec-generalstats/{project}/{username}", name="EditCounterGeneralStats")
114
     */
115 View Code Duplication
    public function generalStatsAction($project, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $this->setUpEditCounter($project, $username);
118
119
//        $revisionCounts = $this->editCounterHelper->getRevisionCounts($user->getId($project));
1 ignored issue
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
//        $pageCounts = $this->editCounterHelper->getPageCounts($username, $revisionCounts['total']);
121
//        $logCounts = $this->editCounterHelper->getLogCounts($user->getId($project));
122
//        $automatedEditsSummary = $automatedEditsHelper->getEditsSummary($user->getId($project));
123
//        $topProjectsEditCounts = $this->editCounterHelper->getTopProjectsEditCounts($project->getUrl(), 
124
//            $user->getUsername());
125
126
        // Render view.
127
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
128
        return $this->render('editCounter/general_stats.html.twig', [
129
            'xtPage' => 'ec',
130
            'is_sub_request' => $isSubRequest,
131
            'is_labs' => $this->project->getRepository()->isLabs(),
132
            'user' => $this->user,
133
            'project' => $this->project,
134
            'ec' => $this->editCounter,
135
136
            // Revision counts.
1 ignored issue
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
//            'deleted_edits' => $revisionCounts['deleted'],
138
//            'total_edits' => $revisionCounts['total'],
139
//            'live_edits' => $revisionCounts['live'],
140
//            'first_rev' => $revisionCounts['first'],
141
//            'latest_rev' => $revisionCounts['last'],
142
//            'days' => $revisionCounts['days'],
143
//            'avg_per_day' => $revisionCounts['avg_per_day'],
144
//            'rev_24h' => $revisionCounts['24h'],
145
//            'rev_7d' => $revisionCounts['7d'],
146
//            'rev_30d' => $revisionCounts['30d'],
147
//            'rev_365d' => $revisionCounts['365d'],
148
//            'rev_small' => $revisionCounts['small'],
149
//            'rev_large' => $revisionCounts['large'],
150
//            'with_comments' => $revisionCounts['with_comments'],
151
//            'without_comments' => $revisionCounts['live'] - $revisionCounts['with_comments'],
152
//            'minor_edits' => $revisionCounts['minor_edits'],
153
//            'nonminor_edits' => $revisionCounts['live'] - $revisionCounts['minor_edits'],
154
//            'auto_edits_total' => array_sum($automatedEditsSummary),
155
//
156
//            // Page counts.
157
//            'uniquePages' => $pageCounts['unique'],
158
//            'pagesCreated' => $pageCounts['created'],
159
//            'pagesMoved' => $pageCounts['moved'],
160
//            'editsPerPage' => $pageCounts['edits_per_page'],
161
//
162
//            // Log counts (keys are 'log name'-'action').
163
//            'pagesThanked' => $logCounts['thanks-thank'],
164
//            'pagesApproved' => $logCounts['review-approve'], // Merged -a, -i, and -ia approvals.
165
//            'pagesPatrolled' => $logCounts['patrol-patrol'],
166
//            'usersBlocked' => $logCounts['block-block'],
167
//            'usersUnblocked' => $logCounts['block-unblock'],
168
//            'pagesProtected' => $logCounts['protect-protect'],
169
//            'pagesUnprotected' => $logCounts['protect-unprotect'],
170
//            'pagesDeleted' => $logCounts['delete-delete'],
171
//            'pagesDeletedRevision' => $logCounts['delete-revision'],
172
//            'pagesRestored' => $logCounts['delete-restore'],
173
//            'pagesImported' => $logCounts['import-import'],
174
//            'files_uploaded' => $logCounts['upload-upload'],
175
//            'files_modified' => $logCounts['upload-overwrite'],
176
//            'files_uploaded_commons' => $logCounts['files_uploaded_commons'],
177
//
178
//            // Other projects.
179
//            'top_projects_edit_counts' => $topProjectsEditCounts,
180
        ]);
181
    }
182
183
    /**
184
     * @Route("/ec-namespacetotals/{project}/{username}", name="EditCounterNamespaceTotals")
185
     */
186 View Code Duplication
    public function namespaceTotalsAction($project, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
    {
188
        $this->setUpEditCounter($project, $username);
189
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
190
        return $this->render('editCounter/namespace_totals.html.twig', [
191
            'xtTitle' => 'namespacetotals',
192
            'xtPage' => 'ec',
193
            'is_sub_request' => $isSubRequest,
194
            'is_labs' => $this->project->getRepository()->isLabs(),
195
            'user' => $this->user,
196
            'project' => $this->project,
197
            'ec' => $this->editCounter,
198
        ]);
199
    }
200
201
    /**
202
     * @Route("/ec-timecard/{project}/{username}", name="EditCounterTimeCard")
203
     */
204 View Code Duplication
    public function timecardAction($project, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
205
    {
206
        $this->setUpEditCounter($project);
207
        $this->labsHelper->databasePrepare($project);
0 ignored issues
show
Bug introduced by
The property labsHelper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
208
        $username = ucfirst($username);
209
        $isSubRequest = $this->get('request_stack')->getParentRequest() !== null;
210
        $datasets = $this->editCounterHelper->getTimeCard($username);
0 ignored issues
show
Bug introduced by
The property editCounterHelper does not seem to exist. Did you mean editCounter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
211
        return $this->render('editCounter/timecard.html.twig', [
212
            'xtTitle' => 'tool-ec',
213
            'xtPage' => 'ec',
214
            'is_sub_request' => $isSubRequest,
215
            'datasets' => $datasets,
216
        ]);
217
    }
218
219
    /**
220
     * @Route("/ec-yearcounts/{project}/{username}", name="EditCounterYearCounts")
221
     */
222 View Code Duplication
    public function yearcountsAction($project, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
223
    {
224
        $this->setUpEditCounter($project);
225
        $this->labsHelper->databasePrepare($project);
226
        $username = ucfirst($username);
227
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
228
        $yearcounts = $this->editCounterHelper->getYearCounts($username);
0 ignored issues
show
Bug introduced by
The property editCounterHelper does not seem to exist. Did you mean editCounter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
229
        return $this->render('editCounter/yearcounts.html.twig', [
230
            'xtTitle' => 'tool_ec',
231
            'xtPage' => 'ec',
232
            'is_sub_request' => $isSubRequest,
233
            'namespaces' => $this->apiHelper->namespaces($project),
0 ignored issues
show
Bug introduced by
The property apiHelper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
234
            'yearcounts' => $yearcounts,
235
        ]);
236
    }
237
238
    /**
239
     * @Route("/ec-monthcounts/{project}/{username}", name="EditCounterMonthCounts")
240
     */
241 View Code Duplication
    public function monthcountsAction($project, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
242
    {
243
        $this->setUpEditCounter($project);
244
        $this->labsHelper->databasePrepare($project);
245
        $username = ucfirst($username);
246
        $monthlyTotalsByNamespace = $this->editCounterHelper->getMonthCounts($username);
0 ignored issues
show
Bug introduced by
The property editCounterHelper does not seem to exist. Did you mean editCounter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
247
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
248
        return $this->render('editCounter/monthcounts.html.twig', [
249
            'xtTitle' => 'tool_ec',
250
            'xtPage' => 'ec',
251
            'is_sub_request' => $isSubRequest,
252
            'month_counts' => $monthlyTotalsByNamespace,
253
            'namespaces' => $this->apiHelper->namespaces($project),
254
        ]);
255
    }
256
257
    /**
258
     * @Route("/ec-latestglobal/{project}/{username}", name="EditCounterLatestGlobal")
259
     */
260
    public function latestglobalAction($project, $username)
261
    {
262
        $this->setUpEditCounter($project);
263
        $info = $this->labsHelper->databasePrepare($project);
264
        $username = ucfirst($username);
265
266
        $topProjectsEditCounts = $this->editCounterHelper->getTopProjectsEditCounts(
0 ignored issues
show
Bug introduced by
The property editCounterHelper does not seem to exist. Did you mean editCounter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
267
            $info['url'],
268
            $username
269
        );
270
        $recentGlobalContribs = $this->editCounterHelper->getRecentGlobalContribs(
0 ignored issues
show
Bug introduced by
The property editCounterHelper does not seem to exist. Did you mean editCounter?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
271
            $username,
272
            array_keys($topProjectsEditCounts)
273
        );
274
275
        $isSubRequest = $this->container->get('request_stack')->getParentRequest() !== null;
276
        return $this->render('editCounter/latest_global.html.twig', [
277
            'xtTitle' => 'tool_ec',
278
            'xtPage' => 'ec',
279
            'is_sub_request' => $isSubRequest,
280
            'latest_global_contribs' => $recentGlobalContribs,
281
            'username' => $username,
282
            'project' => $project,
283
            'namespaces' => $this->apiHelper->namespaces($project),
284
        ]);
285
    }
286
}
287