Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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\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
15
{
16
    /**
17
     * @Route("/switch-site", name="KunstmaanMultiDomainBundle_switch_site", methods={"GET"})
18
     *
19
     * @param \Symfony\Component\HttpFoundation\Request $request
20
     *
21
     * @return Response
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
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