Passed
Push — master ( cd9417...6e4265 )
by Gabriel
03:32 queued 12s
created

UrlTransformer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 45
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A from() 0 4 1
A transform() 0 14 1
1
<?php
2
3
namespace Nip\Mvc\Sections\UrlTransformer;
4
5
use Nip\Http\Request;
6
7
/**
8
 * Class UrlTransformer
9
 * @package Nip\Mvc\Sections\UrlTransformer
10
 */
11
class UrlTransformer
12
{
13
    /**
14
     * @var Request
15
     */
16
    protected $request = null;
17
18
    /**
19
     * @param Request $request
20
     * @return UrlTransformer
21
     */
22 4
    public static function from(Request $request = null)
23
    {
24 4
        $transformer = new self($request);
25 4
        return $transformer;
26
    }
27
28
    /**
29
     * @param $url
30
     * @param $section
31
     * @return string
32
     */
33 4
    public function transform($url, $section)
34
    {
35 4
        $http = $this->request->getHttp();
36 4
        $httpSubdomain = $http->getSubdomain();
37 4
        $rootDomain = $http->getRootDomain();
38
39 4
        $replace = '://' . $section->getSubdomain() . '.' . $rootDomain;
40
        $transform = [
41 4
            '://' . $httpSubdomain . '.' . $rootDomain => $replace,
42 4
            '://' . $rootDomain => $replace,
43
        ];
44
45 4
        $url = strtr($url, $transform);
46 4
        return $url;
47
    }
48
49
    /**
50
     * UrlTransformer constructor.
51
     * @param Request|null $request
52
     */
53 4
    protected function __construct(Request $request = null)
54
    {
55 4
        $this->request = $request ? $request : request();
56
    }
57
}