Test Failed
Pull Request — release/3.x (#37)
by
unknown
07:22
created

RequestAwareProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
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\Request;
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(Request $request)
22
    {
23
        parent::__construct();
24
25
        $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...
26
        $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...
27
        $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...
28
    }
29
30
    /**
31
     * @{inheritDoc}
32
     */
33
    public function url($object, array $options = array())
34
    {
35
        $ret = parent::url($object, $options);
36
37
        if ($this->baseUrlLen && substr($ret, 0, $this->baseUrlLen) == $this->baseUrl) {
38
            $ret = substr($ret, $this->baseUrlLen);
39
        }
40
        $ret = ltrim($ret, '/');
41
        if (!empty($options['absolute'])) {
42
            $ret = $this->prefix . '/' . $ret;
43
        }
44
        return $ret;
45
    }
46
}
47