Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

BasePathTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 12 2
A getBasePath() 0 5 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service\UriGenerator\Transformer;
11
12
use Psr\Http\Message\UriInterface;
13
use Slick\Http\Uri;
14
use Slick\Mvc\Service\UriGenerator\LocationTransformerInterface;
15
16
/**
17
 * Base Path Transformer
18
 *
19
 * @package Slick\Mvc\Service\UriGenerator\Transformer
20
 * @author  Filipe Silva <[email protected]>
21
 */
22
class BasePathTransformer extends AbstractLocationTransformer implements
23
    LocationTransformerInterface
24
{
25
26
    /**
27
     * Tries to transform the provided location data into a server URI
28
     *
29
     * @param string $location Location name, path or identifier
30
     * @param array $options Filter options
31
     *
32
     * @return UriInterface|null
33
     */
34
    public function transform($location, array $options = [])
35
    {
36
        $this->options = array_merge($this->options, $options);
37
        if (!$this->request) {
38
            return null;
39
        }
40
41
        $path = str_replace('//', '/', $this->getBasePath()."/$location");
42
        $uri = (new Uri())->withPath($path);
43
        $uri = $this->updateOptions($uri);
44
        return $uri;
45
    }
46
47
    /**
48
     * Get the request base path
49
     *
50
     * @return string
51
     */
52
    private function getBasePath()
53
    {
54
        $name = $this->request->getServerParams()['SCRIPT_NAME'];
55
        return preg_replace('/(\w+\.php)/i', '', $name);
56
    }
57
58
}