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

RequestAwareProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
c 0
b 0
f 0
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