Passed
Push — master ( 166648...d6c1bf )
by MusikAnimal
07:19
created

BlameController::__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
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace AppBundle\Controller;
5
6
use AppBundle\Helper\I18nHelper;
7
use AppBundle\Model\Authorship;
8
use AppBundle\Model\Blame;
9
use AppBundle\Repository\BlameRepository;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpFoundation\RequestStack;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * This controller provides the search form and results page for the Blame tool.
17
 * @codeCoverageIgnore
18
 */
19
class BlameController extends XtoolsController
20
{
21
    /**
22
     * Get the name of the tool's index route. This is also the name of the associated model.
23
     * @return string
24
     * @codeCoverageIgnore
25
     */
26
    public function getIndexRoute(): string
27
    {
28
        return 'Blame';
29
    }
30
31
    /**
32
     * BlameController constructor.
33
     * @param RequestStack $requestStack
34
     * @param ContainerInterface $container
35
     * @param I18nHelper $i18n
36
     */
37
    public function __construct(RequestStack $requestStack, ContainerInterface $container, I18nHelper $i18n)
38
    {
39
        // Ensures the requested project is validated against Authorship::SUPPORTED_PROJECTS, and not any valid project.
40
        $this->supportedProjects = Authorship::SUPPORTED_PROJECTS;
41
42
        parent::__construct($requestStack, $container, $i18n);
43
    }
44
45
    /**
46
     * The search form.
47
     * @Route("/blame", name="Blame")
48
     * @Route("/blame/{project}", name="BlameProject")
49
     * @return Response
50
     */
51
    public function indexAction(): Response
52
    {
53
        $this->params['target'] = $this->request->query->get('target', '');
54
55
        if (isset($this->params['project']) && isset($this->params['page'])) {
56
            return $this->redirectToRoute('BlameResult', $this->params);
57
        }
58
59
        if (preg_match('/\d{4}-\d{2}-\d{2}/', $this->params['target'])) {
60
            $show = 'date';
61
        } elseif (is_numeric($this->params['target'])) {
62
            $show = 'id';
63
        } else {
64
            $show = 'latest';
65
        }
66
67
        return $this->render('blame/index.html.twig', array_merge([
68
            'xtPage' => 'Blame',
69
            'xtPageTitle' => 'tool-blame',
70
            'xtSubtitle' => 'tool-blame-desc',
71
            'project' => $this->project,
72
73
            // Defaults that will get overridden if in $params.
74
            'page' => '',
75
            'supportedProjects' => Authorship::SUPPORTED_PROJECTS,
76
        ], $this->params, [
77
            'project' => $this->project,
78
            'show' => $show,
79
            'target' => '',
80
        ]));
81
    }
82
83
    /**
84
     * @Route(
85
     *     "/blame/{project}/{page}/{target}",
86
     *     name="BlameResult",
87
     *     requirements={"target"="|latest|\d+|\d{4}-\d{2}-\d{2}"},
88
     *     defaults={"target"="latest"}
89
     * )
90
     * @param string $target
91
     * @return Response
92
     */
93
    public function resultAction(string $target): Response
94
    {
95
        if (0 !== $this->page->getNamespace()) {
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
        $blameRepo = new BlameRepository();
106
        $blameRepo->setContainer($this->container);
107
        $blame = new Blame($this->page, $this->params['q'], $target);
108
        $blame->setRepository($blameRepo);
109
        $blame->prepareData();
110
111
        return $this->getFormattedResponse('blame/blame', [
112
            'xtPage' => 'Blame',
113
            'xtTitle' => $this->page->getTitle(),
114
            'blame' => $blame,
115
        ]);
116
    }
117
}
118