Test Failed
Push — dependency-injection ( 7565fa...15c3c9 )
by MusikAnimal
04:20
created

GlobalContribsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 10
dl 0
loc 17
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace App\Controller;
6
7
use App\Helper\I18nHelper;
8
use App\Model\Edit;
9
use App\Model\GlobalContribs;
10
use App\Repository\EditRepository;
11
use App\Repository\GlobalContribsRepository;
12
use App\Repository\PageRepository;
13
use App\Repository\ProjectRepository;
14
use App\Repository\UserRepository;
15
use GuzzleHttp\Client;
16
use Psr\Cache\CacheItemPoolInterface;
17
use Psr\Container\ContainerInterface;
18
use Symfony\Component\Finder\Glob;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\RequestStack;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Annotation\Route;
23
24
/**
25
 * This controller serves the search form and results for the Global Contributions tool.
26
 * @codeCoverageIgnore
27
 */
28
class GlobalContribsController extends XtoolsController
29
{
30
    protected GlobalContribsRepository $globalContribsRepo;
31
    protected EditRepository $editRepo;
32
33
    public function __construct(
34
        RequestStack $requestStack,
35
        ContainerInterface $container,
36
        CacheItemPoolInterface $cache,
37
        Client $guzzle,
38
        I18nHelper $i18n,
39
        ProjectRepository $projectRepo,
40
        UserRepository $userRepo,
41
        PageRepository $pageRepo,
42
        GlobalContribsRepository $globalContribsRepo,
43
        EditRepository $editRepo
44
    ) {
45
        // GlobalContribs can be very slow, especially for wide IP ranges, so limit to max 500 results.
46
        $this->maxLimit = 500;
47
        $this->globalContribsRepo = $globalContribsRepo;
48
        $this->editRepo = $editRepo;
49
        parent::__construct($requestStack, $container, $cache, $guzzle, $i18n, $projectRepo, $userRepo, $pageRepo);
50
    }
51
52
    /**
53
     * Get the name of the tool's index route. This is also the name of the associated model.
54
     * @return string
55
     * @codeCoverageIgnore
56
     */
57
    public function getIndexRoute(): string
58
    {
59
        return 'GlobalContribs';
60
    }
61
62
    /**
63
     * The search form.
64
     * @Route("/globalcontribs", name="GlobalContribs")
65
     * @Route("/ec-latestglobal", name="EditCounterLatestGlobalIndex")
66
     * @Route("/ec-latestglobal-contributions", name="EditCounterLatestGlobalContribsIndex")
67
     * @Route("/ec-latestglobaledits", name="EditCounterLatestGlobalEditsIndex")
68
     * @return Response
69
     */
70
    public function indexAction(): Response
71
    {
72
        // Redirect if username is given.
73
        if (isset($this->params['username'])) {
74
            return $this->redirectToRoute('GlobalContribsResult', $this->params);
75
        }
76
77
        // FIXME: Nasty hack until T226072 is resolved.
78
        $project = $this->projectRepo->getProject($this->i18n->getLang().'.wikipedia');
79
        if (!$project->exists()) {
80
            $project = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
81
        }
82
83
        return $this->render('globalContribs/index.html.twig', array_merge([
84
            'xtPage' => 'GlobalContribs',
85
            'xtPageTitle' => 'tool-globalcontribs',
86
            'xtSubtitle' => 'tool-globalcontribs-desc',
87
            'project' => $project,
88
89
            // Defaults that will get overridden if in $this->params.
90
            'namespace' => 'all',
91
            'start' => '',
92
            'end' => '',
93
        ], $this->params));
94
    }
95
96
    /**
97
     * @return GlobalContribs
98
     */
99
    public function getGlobalContribs(): GlobalContribs
100
    {
101
        return new GlobalContribs(
102
            $this->globalContribsRepo,
103
            $this->pageRepo,
104
            $this->userRepo,
105
            $this->editRepo,
106
            $this->user,
0 ignored issues
show
Bug introduced by
It seems like $this->user can also be of type null; however, parameter $user of App\Model\GlobalContribs::__construct() does only seem to accept App\Model\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            /** @scrutinizer ignore-type */ $this->user,
Loading history...
107
            $this->namespace,
108
            $this->start,
109
            $this->end,
110
            $this->offset,
111
            $this->limit
112
        );
113
    }
114
115
    /**
116
     * Display the latest global edits tool. First two routes are legacy.
117
     * @Route(
118
     *     "/ec-latestglobal-contributions/{project}/{username}",
119
     *     name="EditCounterLatestGlobalContribs",
120
     *     requirements={
121
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
122
     *     },
123
     *     defaults={
124
     *         "project"="",
125
     *         "namespace"="all"
126
     *     }
127
     * )
128
     * @Route(
129
     *     "/ec-latestglobal/{project}/{username}",
130
     *     name="EditCounterLatestGlobal",
131
     *     requirements={
132
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
133
     *     },
134
     *     defaults={
135
     *         "project"="",
136
     *         "namespace"="all"
137
     *     }
138
     * ),
139
     * @Route(
140
     *     "/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
141
     *     name="GlobalContribsResult",
142
     *     requirements={
143
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
144
     *         "namespace"="|all|\d+",
145
     *         "start"="|\d*|\d{4}-\d{2}-\d{2}",
146
     *         "end"="|\d{4}-\d{2}-\d{2}",
147
     *         "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
148
     *     },
149
     *     defaults={
150
     *         "namespace"="all",
151
     *         "start"=false,
152
     *         "end"=false,
153
     *         "offset"=false,
154
     *     }
155
     * ),
156
     * @return Response
157
     * @codeCoverageIgnore
158
     */
159
    public function resultsAction(): Response
160
    {
161
        $globalContribs = $this->getGlobalContribs();
162
        $defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
163
164
        return $this->render('globalContribs/result.html.twig', [
165
            'xtTitle' => $this->user->getUsername(),
0 ignored issues
show
Bug introduced by
The method getUsername() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
            'xtTitle' => $this->user->/** @scrutinizer ignore-call */ getUsername(),

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...
166
            'xtPage' => 'GlobalContribs',
167
            'is_sub_request' => $this->isSubRequest,
168
            'user' => $this->user,
169
            'project' => $defaultProject,
170
            'gc' => $globalContribs,
171
        ]);
172
    }
173
174
    /************************ API endpoints ************************/
175
176
    /**
177
     * Get global edits made by a user, IP or IP range.
178
     * @Route(
179
     *     "/api/user/globalcontribs/{username}/{namespace}/{start}/{end}/{offset}",
180
     *     name="UserApiGlobalContribs",
181
     *     requirements={
182
     *         "username"="(ipr-.+\/\d+[^\/])|([^\/]+)",
183
     *         "namespace"="|all|\d+",
184
     *         "start"="|\d*|\d{4}-\d{2}-\d{2}",
185
     *         "end"="|\d{4}-\d{2}-\d{2}",
186
     *         "offset"="|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}",
187
     *     },
188
     *     defaults={
189
     *         "namespace"="all",
190
     *         "start"=false,
191
     *         "end"=false,
192
     *         "offset"=false,
193
     *         "limit"=50,
194
     *     },
195
     * )
196
     * @return JsonResponse
197
     */
198
    public function resultsApiAction(): JsonResponse
199
    {
200
        $this->recordApiUsage('user/globalcontribs');
201
202
        $globalContribs = $this->getGlobalContribs();
203
        $defaultProject = $this->projectRepo->getProject($this->getParameter('central_auth_project'));
204
        $this->project = $defaultProject;
205
206
        $results = $globalContribs->globalEdits();
207
        $results = array_map(function (Edit $edit) {
208
            return $edit->getForJson(true, true);
209
        }, array_values($results));
210
        $results = $this->addFullPageTitlesAndContinue('globalcontribs', [], $results);
211
212
        return $this->getFormattedApiResponse($results);
213
    }
214
}
215