TenantAwareRouter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 7
c 2
b 1
f 1
lcom 1
cbo 3
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B generateUrl() 0 26 6
1
<?php
2
3
4
namespace Tahoe\Bundle\MultiTenancyBundle\Service;
5
6
use Symfony\Component\HttpFoundation\RequestStack;
7
use Symfony\Component\Routing\Router;
8
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface;
9
10
/**
11
 * Class TenantAwareRouter that cares about tenants! :-)
12
 *
13
 * @author Konrad Podgórski <[email protected]>
14
 */
15
class TenantAwareRouter
16
{
17
    const ABSOLUTE_URL_FALSE = false;
18
    /**
19
     * @var RequestStack
20
     */
21
    protected $requestStack;
22
    /**
23
     * @var string
24
     */
25
    protected $domain;
26
    /**
27
     * @var Router
28
     */
29
    protected $router;
30
31
    protected $strategy;
32
33
    protected $redirectRoute;
34
35
    function __construct($requestStack, $domain , $router, $strategy, $route = '')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
    {
37
        $this->requestStack = $requestStack;
38
        $this->domain = $domain;
39
        $this->router = $router;
40
        $this->strategy = $strategy;
41
        $this->redirectRoute = $route;
42
    }
43
44
    /**
45
     * @param MultiTenantTenantInterface $tenant
46
     * @param string                           $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47
     * @param array                            $parameters
48
     *
49
     * @return string
50
     */
51
    public function generateUrl(MultiTenantTenantInterface $tenant, $parameters = array())
52
    {
53
        $url = '';
0 ignored issues
show
Unused Code introduced by
$url is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
54
        $name = $this->redirectRoute;
55
56
        if ($this->strategy == TenantResolver::STRATEGY_TENANT_AWARE_SUBDOMAIN ) {
57
            $scheme = $this->requestStack->getCurrentRequest()->getScheme();
58
            $requestPort = $this->requestStack->getCurrentRequest()->getPort();
59
60
            $host = $scheme . '://' . $tenant->getSubdomain() . '.' . $this->domain;
61
62
            $port = '';
63
            if ('http' === $scheme && 80 != $requestPort) {
64
                $port = ':'.$requestPort;
65
            } elseif ('https' === $scheme && 443 != $requestPort) {
66
                $port = ':'.$requestPort;
67
            }
68
69
            $url = $host . $port;
70
            $url .= $this->router->generate($name, $parameters, self::ABSOLUTE_URL_FALSE);
0 ignored issues
show
Documentation introduced by
self::ABSOLUTE_URL_FALSE 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...
71
72
            return $url;
73
        } else {
74
            return $this->router->generate($name, $parameters);
75
        }
76
    }
77
}
78