Passed
Push — master ( 16bc58...e2c4be )
by MusikAnimal
05:53
created

AuthorshipController::indexAction()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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