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

AuthorshipController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 42
c 2
b 0
f 0
dl 0
loc 106
rs 10
wmc 11

4 Methods

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