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

UrlTransformer::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
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
}