Passed
Push — master ( 6bacca...4a4b10 )
by MusikAnimal
05:38
created

BlameController::indexAction()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 20
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 29
rs 8.9777
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
     * @Route("/blame/{project}/{page}", name="BlameProjectPage")
50
     * @return Response
51
     */
52
    public function indexAction(): Response
53
    {
54
        $this->params['target'] = $this->request->query->get('target', '');
55
56
        if (isset($this->params['project']) && isset($this->params['page']) && isset($this->params['q'])) {
57
            return $this->redirectToRoute('BlameResult', $this->params);
58
        }
59
60
        if (preg_match('/\d{4}-\d{2}-\d{2}/', $this->params['target'])) {
61
            $show = 'date';
62
        } elseif (is_numeric($this->params['target'])) {
63
            $show = 'id';
64
        } else {
65
            $show = 'latest';
66
        }
67
68
        return $this->render('blame/index.html.twig', array_merge([
69
            'xtPage' => 'Blame',
70
            'xtPageTitle' => 'tool-blame',
71
            'xtSubtitle' => 'tool-blame-desc',
72
            'project' => $this->project,
73
74
            // Defaults that will get overridden if in $params.
75
            'page' => '',
76
            'supportedProjects' => Authorship::SUPPORTED_PROJECTS,
77
        ], $this->params, [
78
            'project' => $this->project,
79
            'show' => $show,
80
            'target' => '',
81
        ]));
82
    }
83
84
    /**
85
     * @Route(
86
     *     "/blame/{project}/{page}/{target}",
87
     *     name="BlameResult",
88
     *     requirements={"target"="|latest|\d+|\d{4}-\d{2}-\d{2}"},
89
     *     defaults={"target"="latest"}
90
     * )
91
     * @param string $target
92
     * @return Response
93
     */
94
    public function resultAction(string $target): Response
95
    {
96
        if (0 !== $this->page->getNamespace()) {
97
            $this->addFlashMessage('danger', 'error-authorship-non-mainspace');
98
            return $this->redirectToRoute('BlameProject', [
99
                'project' => $this->project->getDomain(),
100
            ]);
101
        }
102
103
        // This action sometimes requires more memory. 256M should be safe.
104
        ini_set('memory_limit', '256M');
105
106
        $blameRepo = new BlameRepository();
107
        $blameRepo->setContainer($this->container);
108
        $blame = new Blame($this->page, $this->params['q'], $target);
109
        $blame->setRepository($blameRepo);
110
        $blame->prepareData();
111
112
        return $this->getFormattedResponse('blame/blame', [
113
            'xtPage' => 'Blame',
114
            'xtTitle' => $this->page->getTitle(),
115
            'blame' => $blame,
116
        ]);
117
    }
118
}
119