AuthorshipController::supportedProjects()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

104
        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...
105
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
106
            return $this->redirectToRoute('AuthorshipProject', [
107
                'project' => $this->project->getDomain(),
108
            ]);
109
        }
110
111
        // This action sometimes requires more memory. 256M should be safe.
112
        ini_set('memory_limit', '256M');
113
114
        $isSubRequest = $this->request->get('htmlonly') || null !== $requestStack->getParentRequest();
115
        $limit = $isSubRequest ? 10 : ($this->limit ?? 500);
116
117
        $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

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