Passed
Pull Request — release/4.x (#44)
by Erik
07:43 queued 03:55
created

RequestAwareProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 33
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 12 4
A __construct() 0 9 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\RequestStack;
10
11
/**
12
 * This provider is "request aware", so it can either render absolute URL's
13
 */
14
class RequestAwareProvider extends DelegatingProvider
15
{
16
    /**
17
     * Constructor
18
     *
19
     * @param \Symfony\Component\HttpFoundation\Request $request
20
     */
21
    public function __construct(RequestStack $requestStack)
22
    {
23
        parent::__construct();
24
25
        $request = $requestStack->getMasterRequest();
26
27
        $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...
28
        $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...
29
        $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...
30
    }
31
32
    /**
33
     * @{inheritDoc}
34
     */
35
    public function url($object, array $options = [])
36
    {
37
        $ret = parent::url($object, $options);
38
39
        if ($this->baseUrlLen && substr($ret, 0, $this->baseUrlLen) == $this->baseUrl) {
40
            $ret = substr($ret, $this->baseUrlLen);
41
        }
42
        $ret = ltrim($ret, '/');
43
        if (!empty($options['absolute'])) {
44
            $ret = $this->prefix . '/' . $ret;
45
        }
46
        return $ret;
47
    }
48
}
49