Completed
Push — master ( 6a1503...7e8e90 )
by Axel
05:21
created

MainController::homeAction()   C

Complexity

Conditions 13
Paths 28

Size

Total Lines 46
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 30
c 0
b 0
f 0
nc 28
nop 1
dl 0
loc 46
rs 6.6166

How to fix   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\Bundle\CoreBundle\Controller;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\HttpKernelInterface;
19
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
20
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
21
22
/**
23
 * Main controller.
24
 */
25
class MainController
26
{
27
    /**
28
     * @var ZikulaHttpKernelInterface
29
     */
30
    private $kernel;
31
32
    /**
33
     * @var VariableApiInterface
34
     */
35
    private $variableApi;
36
37
    public function __construct(
38
        ZikulaHttpKernelInterface $kernel,
39
        VariableApiInterface $variableApi
40
    ) {
41
        $this->kernel = $kernel;
42
        $this->variableApi = $variableApi;
43
    }
44
45
    /**
46
     * This controller action is designed for the "/" route (home).
47
     * The route definition is set in `CoreBundle/Resources/config/routing.xml`
48
     */
49
    public function homeAction(Request $request): Response
50
    {
51
        $startPageInfo = $this->variableApi->getSystemVar('startController');
52
        if (!is_array($startPageInfo) || !isset($startPageInfo['controller']) || empty($startPageInfo['controller'])) {
53
            return new Response(''); // home page is static
54
        }
55
56
        $isValidStartController = true;
57
        [$route, $controller] = explode('###', $startPageInfo['controller']);
58
        if (false === mb_strpos($controller, '\\') || false === mb_strpos($controller, '::')) {
59
            $isValidStartController = false;
60
        } else {
61
            [$vendor, $extensionName] = explode('\\', $controller);
62
            $extensionName = $vendor . $extensionName;
63
            [$fqcn, $method] = explode('::', $controller);
64
            if (!$this->kernel->isBundle($extensionName) || !class_exists($fqcn) || !is_callable([$fqcn, $method])) {
65
                $isValidStartController = false;
66
            }
67
        }
68
69
        if (!$isValidStartController) {
70
            return new Response(''); // home page is static
71
        }
72
73
        $queryParams = $requestParams = $attributes = [];
74
        if (null !== $startPageInfo['query']) {
75
            parse_str($startPageInfo['query'], $queryParams);
76
        }
77
        if (null !== $startPageInfo['request']) {
78
            parse_str($startPageInfo['request'], $requestParams);
79
        }
80
        if (null !== $startPageInfo['attributes']) {
81
            parse_str($startPageInfo['attributes'], $attributes);
82
        }
83
        $attributes['_controller'] = $controller;
84
        $attributes['_route'] = $route;
85
86
        $subRequest = $request->duplicate($queryParams, $requestParams, $attributes);
87
88
        $subRequest->attributes->set('_zkBundle', $extensionName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $extensionName does not seem to be defined for all execution paths leading up to this point.
Loading history...
89
        $subRequest->attributes->set('_zkModule', $extensionName);
90
        // fix for #3929, #3932
91
        $request->attributes->set('_zkBundle', $extensionName);
92
        $request->attributes->set('_zkModule', $extensionName);
93
94
        return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
95
    }
96
}
97