UrlUtils   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 48
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getRequest() 0 1 ?
A getRouterGenerator() 0 6 1
B getUrl() 0 22 4
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\Controller;
11
12
use Aura\Router\Exception as AuraException;
13
use Slick\Http\PhpEnvironment\Request;
14
use Slick\Mvc\Application;
15
use Slick\Mvc\Router;
16
17
/**
18
 * UrlUtils
19
 * 
20
 * @package Slick\Mvc\Controller
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
trait UrlUtils
24
{
25
    /**
26
     * @return Request
27
     */
28
    abstract public function getRequest();
29
30
    /**
31
     * Gets the url for provided path
32
     * 
33
     * @param string $path
34
     * @return string
35
     */
36 4
    public function getUrl($path)
37
    {
38 4
        $regExp = '/\/\/|https?:/i';
39 4
        if (preg_match($regExp, $path)) {
40 2
            return $path;
41
        }
42
        try {
43 2
            $generated = call_user_func_array(
44 2
                [$this->getRouterGenerator(), 'generate'],
45 1
                func_get_args()
46 1
            );
47 1
        } catch (AuraException $caught) {
48
            $generated = false;
49
        }
50
51 2
        $basePath = rtrim($this->getRequest()->getBasePath(), '/');
52 1
        $path = $generated
53 1
            ? $generated
54 2
            : "{$basePath}/{$path}";
55
56 2
        return $path;
57
    }
58
59
    /**
60
     * Return Router path generator
61
     *
62
     * @return \Aura\Router\Generator
63
     */
64 2
    protected function getRouterGenerator()
65
    {
66
        /** @var Router $router */
67 2
        $router = Application::container()->get('router.middleware');
68 2
        return $router->getRouterContainer()->getGenerator();
69
    }
70
}