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

RequestAwareProvider   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A url() 0 12 4
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