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

BlameController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIndexRoute() 0 3 1
A resultAction() 0 25 3
A supportedProjects() 0 3 1
A indexAction() 0 28 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Model\Authorship;
8
use App\Model\Blame;
9
use App\Repository\BlameRepository;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
/**
14
 * This controller provides the search form and results page for the Blame tool.
15
 * @codeCoverageIgnore
16
 */
17
class BlameController extends XtoolsController
18
{
19
    /**
20
     * Get the name of the tool's index route. This is also the name of the associated model.
21
     * @return string
22
     * @codeCoverageIgnore
23
     */
24
    public function getIndexRoute(): string
25
    {
26
        return 'Blame';
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public function supportedProjects(): array
33
    {
34
        return Authorship::SUPPORTED_PROJECTS;
35
    }
36
37
    /**
38
     * The search form.
39
     * @Route("/blame", name="Blame")
40
     * @Route("/blame/{project}", name="BlameProject")
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']) && isset($this->params['q'])) {
48
            return $this->redirectToRoute('BlameResult', $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('blame/index.html.twig', array_merge([
60
            'xtPage' => 'Blame',
61
            'xtPageTitle' => 'tool-blame',
62
            'xtSubtitle' => 'tool-blame-desc',
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
     *     "/blame/{project}/{page}/{target}",
77
     *     name="BlameResult",
78
     *     requirements={
79
     *         "page"="(.+?)",
80
     *         "target"="|latest|\d+|\d{4}-\d{2}-\d{2}",
81
     *     },
82
     *     defaults={"target"="latest"}
83
     * )
84
     * @param string $target
85
     * @param BlameRepository $blameRepo
86
     * @return Response
87
     */
88
    public function resultAction(string $target, BlameRepository $blameRepo): Response
89
    {
90
        if (!isset($this->params['q'])) {
91
            return $this->redirectToRoute('BlameProject', [
92
                'project' => $this->project->getDomain(),
93
            ]);
94
        }
95
        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

95
        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...
96
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
97
            return $this->redirectToRoute('BlameProject', [
98
                'project' => $this->project->getDomain(),
99
            ]);
100
        }
101
102
        // This action sometimes requires more memory. 256M should be safe.
103
        ini_set('memory_limit', '256M');
104
105
        $blame = new Blame($blameRepo, $this->page, $this->params['q'], $target);
0 ignored issues
show
Bug introduced by
It seems like $this->page can also be of type null; however, parameter $page of App\Model\Blame::__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

105
        $blame = new Blame($blameRepo, /** @scrutinizer ignore-type */ $this->page, $this->params['q'], $target);
Loading history...
106
        $blame->setRepository($blameRepo);
107
        $blame->prepareData();
108
109
        return $this->getFormattedResponse('blame/blame', [
110
            'xtPage' => 'Blame',
111
            'xtTitle' => $this->page->getTitle(),
112
            'blame' => $blame,
113
        ]);
114
    }
115
}
116