Completed
Push — master ( caeb59...e7b0db )
by Axel
05:20
created

HelpController::indexAction()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 77
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 40
nc 9
nop 6
dl 0
loc 77
rs 7.6666
c 0
b 0
f 0

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 - 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 index(
39
        ZikulaHttpKernelInterface $kernel,
40
        Request $request,
41
        Filesystem $fileSystem,
42
        RouterInterface $router,
43
        AssetFilter $assetFilter,
44
        string $moduleName
45
    ): Response {
46
        $page = $request->query->get('page', 'README');
47
        if (false !== mb_strpos($page, '..')) {
48
            throw new \Exception('Invalid page "' . $page . '".');
49
        }
50
51
        $locale = $request->getLocale();
52
        $extension = $kernel->getBundle($moduleName);
53
        if (null === $extension) {
54
            throw new \Exception('Invalid extension "' . $moduleName . '".');
55
        }
56
57
        $helpPath = $extension->getPath() . '/Resources/docs/help/';
58
59
        // check if requested page exists
60
        if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) {
61
            if ('en' === $locale) {
62
                throw new \Exception('Invalid page "' . $page . '".');
63
            }
64
            // fallback to English
65
            $locale = 'en';
66
        }
67
        if (!$fileSystem->exists($helpPath . $locale . '/' . $page . '.md')) {
68
            throw new \Exception('Invalid page "' . $page . '".');
69
        }
70
71
        $raw = $request->query->getInt('raw', 0);
72
73
        $content = file_get_contents($helpPath . $locale . '/' . $page . '.md');
74
75
        // rewrite local links
76
        $content = preg_replace_callback(
77
            '/\[(.*?)\]\((.*?)\)/',
78
            function ($match) use ($router, $moduleName, $raw) {
79
                if (false === mb_strpos($match[2], '.md')) {
80
                    return $match[0];
81
                }
82
                if ('http' === mb_substr($match[2], 0, 4)) {
83
                    return $match[0];
84
                }
85
86
                // local link - rewrite
87
                $urlArgs = [
88
                    'moduleName' => $moduleName,
89
                    'page' => trim($match[2], '.md')
90
                ];
91
                if (1 === $raw) {
92
                    $urlArgs['raw'] = 1;
93
                }
94
                $url = $router->generate('zikulaextensionsmodule_help_index', $urlArgs);
95
96
                return '[' . $match[1] . '](' . $url . ')';
97
            },
98
            $content
99
        );
100
101
        $output = $this->renderView('@ZikulaExtensionsModule/Help/page.html.twig', [
102
            'moduleName' => $moduleName,
103
            'content' => $content,
104
            'raw' => $raw
105
        ]);
106
107
        if (1 === $raw) {
108
            $output = $assetFilter->filter($output);
109
            $output = new PlainResponse($output);
110
111
            return $output;
112
        }
113
114
        return new Response($output);
115
    }
116
}
117