RequestAwareProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 12 4
A __construct() 0 7 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Url;
8
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
12
/**
13
 * This provider is "request aware", so it can either render absolute URL's
14
 */
15
class RequestAwareProvider extends DelegatingProvider
16
{
17
    /**
18
     * Constructor
19
     *
20
     * @param \Symfony\Component\HttpFoundation\Request $request
21 1
     */
22
    public function __construct(Request $request)
23 1
    {
24
        parent::__construct();
25 1
26 1
        $this->baseUrl = $request->getBaseUrl();
0 ignored issues
show
Bug Best Practice introduced by
The property baseUrl does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27 1
        $this->prefix = $request->getSchemeAndHttpHost() . $this->baseUrl;
0 ignored issues
show
Bug Best Practice introduced by
The property prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28 1
        $this->baseUrlLen = strlen($this->baseUrl);
0 ignored issues
show
Bug Best Practice introduced by
The property baseUrlLen does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
    }
30
31
    /**
32
     * @{inheritDoc}
33 1
     */
34
    public function url($object, array $options = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
35 1
    {
36
        $ret = parent::url($object, $options, $referenceType);
37 1
38 1
        if ($this->baseUrlLen && substr($ret, 0, $this->baseUrlLen) == $this->baseUrl) {
39
            $ret = substr($ret, $this->baseUrlLen);
40 1
        }
41 1
        $ret = ltrim($ret, '/');
42 1
        if (!empty($options['absolute'])) {
43
            $ret = $this->prefix . '/' . $ret;
44 1
        }
45
        return $ret;
46
    }
47
}
48