Passed
Push — master ( d0265c...1b1a87 )
by MusikAnimal
07:34
created

BlameController::getIndexRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace AppBundle\Controller;
5
6
use AppBundle\Model\Authorship;
7
use AppBundle\Model\Blame;
8
use AppBundle\Repository\BlameRepository;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\Routing\Annotation\Route;
11
12
/**
13
 * This controller provides the search form and results page for the Blame tool.
14
 * @codeCoverageIgnore
15
 */
16
class BlameController extends XtoolsController
17
{
18
    /**
19
     * Get the name of the tool's index route. This is also the name of the associated model.
20
     * @return string
21
     * @codeCoverageIgnore
22
     */
23
    public function getIndexRoute(): string
24
    {
25
        return 'Blame';
26
    }
27
28
    /**
29
     * The search form.
30
     * @Route("/blame", name="Blame")
31
     * @Route("/blame/{project}", name="BlameProject")
32
     * @return Response
33
     */
34
    public function indexAction(): Response
35
    {
36
        $this->params['target'] = $this->request->query->get('target', '');
37
38
        if (isset($this->params['project']) && isset($this->params['page'])) {
39
            return $this->redirectToRoute('BlameResult', $this->params);
40
        }
41
42
        if (preg_match('/\d{4}-\d{2}-\d{2}/', $this->params['target'])) {
43
            $show = 'date';
44
        } elseif (is_numeric($this->params['target'])) {
45
            $show = 'id';
46
        } else {
47
            $show = 'latest';
48
        }
49
50
        return $this->render('blame/index.html.twig', array_merge([
51
            'xtPage' => 'Blame',
52
            'xtPageTitle' => 'tool-blame',
53
            'xtSubtitle' => 'tool-blame-desc',
54
            'project' => $this->project,
55
56
            // Defaults that will get overridden if in $params.
57
            'page' => '',
58
            'supportedProjects' => Authorship::SUPPORTED_PROJECTS,
59
        ], $this->params, [
60
            'project' => $this->project,
61
            'show' => $show,
62
            'target' => '',
63
        ]));
64
    }
65
66
    /**
67
     * @Route(
68
     *     "/blame/{project}/{page}/{target}",
69
     *     name="BlameResult",
70
     *     requirements={"target"="|latest|\d+|\d{4}-\d{2}-\d{2}"},
71
     *     defaults={"target"="latest"}
72
     * )
73
     * @param string $target
74
     * @return Response
75
     */
76
    public function resultAction(string $target): Response
77
    {
78
        if (0 !== $this->page->getNamespace()) {
79
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
80
            return $this->redirectToRoute('BlameProject', [
81
                'project' => $this->project->getDomain(),
82
            ]);
83
        }
84
        if (!in_array($this->project->getDomain(), Authorship::SUPPORTED_PROJECTS)) {
85
            $this->addFlashMessage('danger', 'error-authorship-unsupported-project', [
86
                $this->project->getDomain(),
87
            ]);
88
            return $this->redirectToRoute('BlameProject', [
89
                'project' => $this->project->getDomain(),
90
            ]);
91
        }
92
93
        // This action sometimes requires more memory. 256M should be safe.
94
        ini_set('memory_limit', '256M');
95
96
        $blameRepo = new BlameRepository();
97
        $blameRepo->setContainer($this->container);
98
        $blame = new Blame($this->page, $this->params['q'], $target);
99
        $blame->setRepository($blameRepo);
100
        $blame->prepareData();
101
102
        return $this->getFormattedResponse('blame/blame', [
103
            'xtPage' => 'Blame',
104
            'xtTitle' => $this->page->getTitle(),
105
            'blame' => $blame,
106
        ]);
107
    }
108
}
109