Passed
Pull Request — main (#442)
by MusikAnimal
08:15 queued 04:14
created

AuthorshipController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Model\Authorship;
8
use App\Repository\AuthorshipRepository;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
/**
13
 * This controller serves the search form and results for the Authorship tool
14
 * @codeCoverageIgnore
15
 */
16
class AuthorshipController extends XtoolsController
17
{
18
    /**
19
     * @inheritDoc
20
     * @codeCoverageIgnore
21
     */
22
    public function getIndexRoute(): string
23
    {
24
        return 'Authorship';
25
    }
26
27
    /**
28
     * @inheritDoc
29
     * @codeCoverageIgnore
30
     */
31
    public function supportedProjects(): array
32
    {
33
        return Authorship::SUPPORTED_PROJECTS;
34
    }
35
36
    /**
37
     * The search form.
38
     * @Route("/authorship", name="Authorship")
39
     * @Route("/authorship/{project}", name="AuthorshipProject")
40
     * @return Response
41
     */
42
    public function indexAction(): Response
43
    {
44
        $this->params['target'] = $this->request->query->get('target', '');
45
46
        if (isset($this->params['project']) && isset($this->params['page'])) {
47
            return $this->redirectToRoute('AuthorshipResult', $this->params);
48
        }
49
50
        if (preg_match('/\d{4}-\d{2}-\d{2}/', $this->params['target'])) {
51
            $show = 'date';
52
        } elseif (is_numeric($this->params['target'])) {
53
            $show = 'id';
54
        } else {
55
            $show = 'latest';
56
        }
57
58
        return $this->render('authorship/index.html.twig', array_merge([
59
            'xtPage' => 'Authorship',
60
            'xtPageTitle' => 'tool-authorship',
61
            'xtSubtitle' => 'tool-authorship-desc',
62
            'project' => $this->project,
63
64
            // Defaults that will get overridden if in $params.
65
            'page' => '',
66
            'supportedProjects' => Authorship::SUPPORTED_PROJECTS,
67
        ], $this->params, [
68
            'project' => $this->project,
69
            'show' => $show,
70
            'target' => '',
71
        ]));
72
    }
73
74
    /**
75
     * @Route(
76
     *     "/articleinfo-authorship/{project}/{page}",
77
     *     name="AuthorshipResultLegacy",
78
     *     requirements={
79
     *         "page"="(.+?)",
80
     *         "target"="|latest|\d+|\d{4}-\d{2}-\d{2}",
81
     *     },
82
     *     defaults={"target"="latest"}
83
     * )
84
     * @Route(
85
     *     "/authorship/{project}/{page}/{target}",
86
     *     name="AuthorshipResult",
87
     *     requirements={
88
     *         "page"="(.+?)",
89
     *         "target"="|latest|\d+|\d{4}-\d{2}-\d{2}",
90
     *     },
91
     *     defaults={"target"="latest"}
92
     * )
93
     * @param string $target
94
     * @param AuthorshipRepository $authorshipRepo
95
     * @return Response
96
     */
97
    public function resultAction(string $target, AuthorshipRepository $authorshipRepo): Response
98
    {
99
        if (0 !== $this->page->getNamespace()) {
0 ignored issues
show
Bug introduced by
The method getNamespace() 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

99
        if (0 !== $this->page->/** @scrutinizer ignore-call */ getNamespace()) {

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...
100
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
101
            return $this->redirectToRoute('AuthorshipProject', [
102
                'project' => $this->project->getDomain(),
103
            ]);
104
        }
105
106
        // This action sometimes requires more memory. 256M should be safe.
107
        ini_set('memory_limit', '256M');
108
109
        $isSubRequest = $this->request->get('htmlonly')
110
            || null !== $this->get('request_stack')->getParentRequest();
111
        $limit = $isSubRequest ? 10 : ($this->limit ?? 500);
112
113
        $authorship = new Authorship($authorshipRepo, $this->page, $target, $limit);
0 ignored issues
show
Bug introduced by
It seems like $this->page can also be of type null; however, parameter $page of App\Model\Authorship::__construct() does only seem to accept App\Model\Page, 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

113
        $authorship = new Authorship($authorshipRepo, /** @scrutinizer ignore-type */ $this->page, $target, $limit);
Loading history...
114
        $authorship->prepareData();
115
116
        return $this->getFormattedResponse('authorship/authorship', [
117
            'xtPage' => 'Authorship',
118
            'xtTitle' => $this->page->getTitle(),
119
            'authorship' => $authorship,
120
            'is_sub_request' => $isSubRequest,
121
        ]);
122
    }
123
}
124