SiteSwitchController::switchAction()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 29
cp 0
rs 8.6737
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
3
namespace Kunstmaan\MultiDomainBundle\Controller;
4
5
use Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration;
6
use Symfony\Component\Routing\Annotation\Route;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\RedirectResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
14
class SiteSwitchController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
15
{
16
    /**
17
     * @Route("/switch-site", name="KunstmaanMultiDomainBundle_switch_site", methods={"GET"})
18
     *
19
     * @param \Symfony\Component\HttpFoundation\Request $request
20
     *
21
     * @return Response
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use RedirectResponse.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
22
     *
23
     * @throws AccessDeniedException
24
     * @throws NotFoundHttpException
25
     */
26
    public function switchAction(Request $request)
27
    {
28
        $domainConfiguration = $this->get('kunstmaan_admin.domain_configuration');
29
        $host = $request->query->get('host');
30
        $hosts = $domainConfiguration->getFullHostConfig();
31
        if (!\array_key_exists($host, $hosts)) {
32
            throw $this->createNotFoundException('Invalid host specified');
33
        }
34
35
        $currentHost = $domainConfiguration->getHost();
36
37
        $session = $request->getSession();
38
        if ($request->get('from_url_chooser')) {
39
            $session->set(DomainConfiguration::SWITCH_HOST, $host);
40
        } else {
41
            $session->set(DomainConfiguration::OVERRIDE_HOST, $host);
42
        }
43
44
        /*
45
         * If current host type is different then the host going to, redirect to it's homepage.
46
         * If coming from url chooser, don't redirect to homepage if other host.
47
         */
48
        if ((($hosts[$host]['type'] !== $hosts[$currentHost]['type']) || (!$request->query->has('route'))) && (!$request->get('from_url_chooser'))) {
49
            $route = 'KunstmaanAdminBundle_homepage';
50
            $defaultLocale = $this->get('kunstmaan_admin.domain_configuration')->getDefaultLocale();
51
        } else {
52
            $route = $request->query->get('route');
53
            $routeParams = $request->query->get('route_params');
54
            $defaultLocale = $hosts[$host]['default_locale'];
55
        }
56
57
        $routeParams['_locale'] = $defaultLocale;
0 ignored issues
show
Bug introduced by
The variable $routeParams does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
59
        $response = new RedirectResponse(
60
            $this->get('router')->generate($route, $routeParams)
61
        );
62
63
        return $response;
64
    }
65
}
66