AgnosticUrlGenerator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
ccs 0
cts 27
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setContext() 0 9 1
A getContext() 0 4 1
A generate() 0 12 1
1
<?php
2
3
namespace Majora\Framework\Routing;
4
5
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6
use Symfony\Component\Routing\RequestContext;
7
8
/**
9
 * Base routing proxy which strips debug base urls in order
10
 * to handle externals sources even with custom dev front controllers
11
 */
12
class AgnosticUrlGenerator implements UrlGeneratorInterface
13
{
14
    /**
15
     * @var UrlGeneratorInterface
16
     */
17
    protected $urlGenerator;
18
19
    /**
20
     * @var RequestContext
21
     */
22
    protected $context;
23
24
    /**
25
     * Construct
26
     *
27
     * @param UrlGeneratorInterface $urlGenerator
28
     */
29
    public function __construct(UrlGeneratorInterface $urlGenerator)
30
    {
31
        $this->urlGenerator = $urlGenerator;
32
        $this->setContext($urlGenerator->getContext());
33
    }
34
35
    /**
36
     * @see RequestContextAwareInterface::setContext()
37
     */
38
    public function setContext(RequestContext $context)
39
    {
40
        $this->context = clone $context;
41
        $this->context->setBaseUrl(preg_replace(
42
            '/(\\/[\w]+_dev.php)/',
43
            '',
44
            $context->getBaseUrl()
45
        ));
46
    }
47
48
    /**
49
     * @see RequestContextAwareInterface::getContext()
50
     */
51
    public function getContext()
52
    {
53
        return $this->context;
54
    }
55
56
    /**
57
     * @see UrlGeneratorInterface::generate()
58
     */
59
    public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
60
    {
61
        $proxiedContext = $this->urlGenerator->getContext();
62
        $this->setContext($proxiedContext);
63
64
        $this->urlGenerator->setContext($this->getContext());
65
        $url = $this->urlGenerator->generate($name, $parameters, $referenceType);
66
67
        $this->urlGenerator->setContext($proxiedContext);
68
69
        return $url;
70
    }
71
}
72