Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Controller/SiteSwitchController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MultiDomainBundle\Controller;
4
5
use Kunstmaan\MultiDomainBundle\Helper\DomainConfiguration;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Symfony\Component\Routing\Annotation\Route;
12
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13
14
class SiteSwitchController extends Controller
15
{
16
    /**
17
     * @Route("/switch-site", name="KunstmaanMultiDomainBundle_switch_site", methods={"GET"})
18
     *
19
     * @return Response
20
     *
21
     * @throws AccessDeniedException
22
     * @throws NotFoundHttpException
23
     */
24
    public function switchAction(Request $request)
25
    {
26
        $domainConfiguration = $this->get('kunstmaan_admin.domain_configuration');
27
        $host = $request->query->get('host');
28
        $hosts = $domainConfiguration->getFullHostConfig();
29
        if (!\array_key_exists($host, $hosts)) {
30
            throw $this->createNotFoundException('Invalid host specified');
31
        }
32
33
        $currentHost = $domainConfiguration->getHost();
34
35
        $session = $request->getSession();
36
        if ($request->get('from_url_chooser')) {
37
            $session->set(DomainConfiguration::SWITCH_HOST, $host);
38
        } else {
39
            $session->set(DomainConfiguration::OVERRIDE_HOST, $host);
40
        }
41
42
        /*
43
         * If current host type is different then the host going to, redirect to it's homepage.
44
         * If coming from url chooser, don't redirect to homepage if other host.
45
         */
46
        if ((($hosts[$host]['type'] !== $hosts[$currentHost]['type']) || (!$request->query->has('route'))) && (!$request->get('from_url_chooser'))) {
47
            $route = 'KunstmaanAdminBundle_homepage';
48
            $defaultLocale = $this->get('kunstmaan_admin.domain_configuration')->getDefaultLocale();
49
        } else {
50
            $route = $request->query->get('route');
51
            $routeParams = $request->query->get('route_params');
52
            $defaultLocale = $hosts[$host]['default_locale'];
53
        }
54
55
        $routeParams['_locale'] = $defaultLocale;
0 ignored issues
show
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...
56
57
        $response = new RedirectResponse(
58
            $this->get('router')->generate($route, $routeParams)
59
        );
60
61
        return $response;
62
    }
63
}
64