Completed
Push — master ( a8108c...0038c7 )
by Axel
14:25 queued 08:34
created

HelpController::indexAction()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 78
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 40
c 1
b 0
f 0
nc 9
nop 6
dl 0
loc 78
rs 7.6666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ExtensionsModule\Controller;
15
16
use Symfony\Component\Filesystem\Filesystem;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Symfony\Component\Routing\RouterInterface;
21
use Zikula\Bundle\CoreBundle\Controller\AbstractController;
22
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
23
use Zikula\Bundle\CoreBundle\Response\PlainResponse;
24
use Zikula\ThemeModule\Engine\Annotation\Theme;
25
use Zikula\ThemeModule\Engine\AssetFilter;
26
27
/**
28
 * @Route("/help")
29
 */
30
class HelpController extends AbstractController
31
{
32
    /**
33
     * @Route("/{moduleName}")
34
     * @Theme("admin")
35
     *
36
     * Display a module's help page.
37
     */
38
    public function indexAction(
39
        ZikulaHttpKernelInterface $kernel,
40
        Request $request,
41
        Filesystem $fileSystem,
42
        RouterInterface $router,
43
        AssetFilter $assetFilter,
44
        string $moduleName
45
    ): Response
46
    {
47
        $page = $request->query->get('page', 'README');
48
        if (false !== mb_strpos($page, '..')) {
49
            throw new \Exception('Invalid page "' . $page . '".');
50
        }
51
52
        $locale = $request->getLocale();
53
        $extension = $kernel->getBundle($moduleName);
54
        if (null === $extension) {
55
            throw new \Exception('Invalid extension "' . $moduleName . '".');
56
        }
57
58
        $helpPath = $extension->getPath() . '/Resources/docs/help/';
59
60
        // check if requested page exists
61
        if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) {
62
            if ('en' === $locale) {
63
                throw new \Exception('Invalid page "' . $page . '".');
64
            }
65
            // fallback to English
66
            $locale = 'en';
67
        }
68
        if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) {
69
            throw new \Exception('Invalid page "' . $page . '".');
70
        }
71
72
        $raw = $request->query->getInt('raw', 0);
73
74
        $content = file_get_contents($helpPath . $locale . '/' . $page . '.md');
75
76
        // rewrite local links
77
        $content = preg_replace_callback(
78
            '/\[(.*?)\]\((.*?)\)/',
79
            function ($match) use($router, $moduleName, $raw) {
80
                if (false === mb_strpos($match[2], '.md')) {
81
                    return $match[0];
82
                }
83
                if ('http' === mb_substr($match[2], 0, 4)) {
84
                    return $match[0];
85
                }
86
87
                // local link - rewrite
88
                $urlArgs = [
89
                    'moduleName' => $moduleName,
90
                    'page' => trim($match[2], '.md')
91
                ];
92
                if (1 === $raw) {
93
                    $urlArgs['raw'] = 1;
94
                }
95
                $url = $router->generate('zikulaextensionsmodule_help_index', $urlArgs);
96
97
                return '[' . $match[1] . '](' . $url . ')';
98
            },
99
            $content
100
        );
101
102
        $output = $this->renderView('@ZikulaExtensionsModule/Help/page.html.twig', [
103
            'moduleName' => $moduleName,
104
            'content' => $content,
105
            'raw' => $raw
106
        ]);
107
108
        if (1 === $raw) {
109
            $output = $assetFilter->filter($output);
110
            $output = new PlainResponse($output);
111
112
            return $output;
113
        }
114
115
        return new Response($output);
116
    }
117
}
118