Passed
Push — up-php7 ( a2fe11...4e1c1e )
by Erik
05:48
created

RequestAwareProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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