RedirectController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Tadcka package.
5
 *
6
 * (c) Tadas Gliaubicas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tadcka\Bundle\RoutingBundle\Controller;
13
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\RouterInterface;
17
use Tadcka\Component\Routing\Model\Manager\RedirectRouteManagerInterface;
18
19
/**
20
 * @author Tadas Gliaubicas <[email protected]>
21
 *
22
 * @since 8/28/14 12:23 PM
23
 */
24
class RedirectController
25
{
26
    /**
27
     * @var RedirectRouteManagerInterface
28
     */
29
    private $redirectRouteManager;
30
31
    /**
32
     * @var RouterInterface
33
     */
34
    private $router;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param RedirectRouteManagerInterface $redirectRouteManager
40
     * @param RouterInterface $router
41
     */
42
    public function __construct(RedirectRouteManagerInterface $redirectRouteManager, RouterInterface $router)
43
    {
44
        $this->router = $router;
45
        $this->redirectRouteManager = $redirectRouteManager;
46
    }
47
48
    /**
49
     * Action to redirect based on a RedirectRouteInterface route.
50
     *
51
     * @param string $redirectRouteName
52
     *
53
     * @return RedirectResponse
54
     *
55
     * @throws NotFoundHttpException
56
     */
57
    public function redirectAction($redirectRouteName)
58
    {
59
        $redirectRoute = $this->redirectRouteManager->findByName($redirectRouteName);
60
        if (null === $redirectRoute) {
61
            throw new NotFoundHttpException();
62
        }
63
64
        $url = $redirectRoute->getUri();
65
66
        if (!$url) {
67
            $routeTarget = $redirectRoute->getRouteTarget();
68
            if (null !== $routeTarget) {
69
                $url = $this->router->generate($routeTarget, $redirectRoute->getParameters(), true);
0 ignored issues
show
Documentation introduced by
$routeTarget is of type object<Tadcka\Component\...g\Model\RouteInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            } else {
71
                $url = $this->router->generate($redirectRoute->getRouteName(), $redirectRoute->getParameters(), true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
            }
73
        }
74
75
        $status = $redirectRoute->isPermanent() ? 301 : 302;
76
77
        return new RedirectResponse($url, $status);
78
    }
79
}
80