Completed
Pull Request — master (#1)
by Rafał
09:02
created

TenantAwareRouter::generate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 8
nc 4
nop 3
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
namespace SWP\MultiTenancyBundle\Routing;
15
16
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
17
use SWP\Component\MultiTenancy\PathBuilder\TenantAwarePathBuilderInterface;
18
19
class TenantAwareRouter extends DynamicRouter
20
{
21
    /**
22
     * @var TenantAwarePathBuilderInterface
23
     */
24
    protected $pathBuilder;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function generate($name, $parameters = array(), $referenceType = false)
30
    {
31
        if (null === $name && isset($parameters['content_id'])) {
32
            $contentId = $this->checkAndRemoveFirstSlash($parameters['content_id']);
33
            $parameters['content_id'] = $this->pathBuilder->build('/', $contentId);
34
        }
35
36
        if (is_string($name)) {
37
            $name = (string) $this->pathBuilder->build(
38
                $this->checkAndRemoveFirstSlash($name)
39
            );
40
        }
41
42
        return parent::generate($name, $parameters, $referenceType);
43
    }
44
45
    private function checkAndRemoveFirstSlash($string)
46
    {
47
        if (substr($string, 0, 1) === '/') {
48
            return substr($string, 1);
49
        }
50
51
        return $string;
52
    }
53
54
    /**
55
     * Sets the tenant aware path builder.
56
     *
57
     * @param TenantAwarePathBuilderInterface $pathBuilder Path builder
58
     */
59
    public function setPathBuilder(TenantAwarePathBuilderInterface $pathBuilder)
60
    {
61
        $this->pathBuilder = $pathBuilder;
62
    }
63
}
64