TenantAwareRouter   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 15 4
A checkAndRemoveFirstSlash() 0 8 2
A setPathBuilder() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher MultiTenancy Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\MultiTenancyBundle\Routing;
16
17
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface;
18
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
19
20
class TenantAwareRouter extends DynamicRouter
21
{
22
    /**
23
     * @var TenantAwarePathBuilderInterface
24
     */
25
    protected $pathBuilder;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function generate($name, $parameters = [], $referenceType = false)
31
    {
32
        if (null === $name && isset($parameters['content_id'])) {
33
            $contentId = $this->checkAndRemoveFirstSlash($parameters['content_id']);
34
            $parameters['content_id'] = $this->pathBuilder->build('/', $contentId);
35
        }
36
37
        if (is_string($name)) {
38
            $name = (string) $this->pathBuilder->build(
39
                $this->checkAndRemoveFirstSlash($name)
40
            );
41
        }
42
43
        return parent::generate($name, $parameters, $referenceType);
44
    }
45
46
    private function checkAndRemoveFirstSlash($string)
47
    {
48
        if ('/' === substr($string, 0, 1)) {
49
            return substr($string, 1);
50
        }
51
52
        return $string;
53
    }
54
55
    /**
56
     * Sets the tenant aware path builder.
57
     *
58
     * @param TenantAwarePathBuilderInterface $pathBuilder Path builder
59
     */
60
    public function setPathBuilder(TenantAwarePathBuilderInterface $pathBuilder)
61
    {
62
        $this->pathBuilder = $pathBuilder;
63
    }
64
}
65