UrlViewHelper   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 12
c 3
b 0
f 3
cbo 8
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
D execute() 0 30 10
A getRequestMatcher() 0 7 1
1
<?php
2
namespace Fwk\Core\Components\RequestMatcher;
3
4
use Fwk\Core\Components\ViewHelper\ViewHelper;
5
use Fwk\Core\Components\ViewHelper\AbstractViewHelper;
6
use Fwk\Core\Components\ViewHelper\Exception;
7
8
class UrlViewHelper extends AbstractViewHelper implements ViewHelper
9
{
10
    protected $serviceName;
11
12
    public function __construct($serviceName)
13
    {
14
        $this->serviceName = $serviceName;
15
    }
16
17
    public function execute(array $arguments)
18
    {
19
        $actionName = (isset($arguments[0]) ? $arguments[0] : false);
20
        $parameters = (isset($arguments[1]) ? $arguments[1] : array());
21
        $escapeAmp  = (isset($arguments[2]) ? (bool)$arguments[2] : false);
22
        $includeHostScheme = (isset($arguments[3]) ? (bool)$arguments[3] : false);
23
24
        $baseUrl    = $this->getViewHelperService()
25
            ->getContext()
26
            ->getRequest()
27
            ->getBaseUrl();
28
29
        if (false === $actionName) {
30
            return (empty($baseUrl) ? '/' : $baseUrl);
31
        }
32
33
        if (empty($actionName)) {
34
            throw new Exception(sprintf('Empty action name'));
35
        }
36
37
        if (!is_array($parameters)) {
38
            throw new Exception(sprintf('Parameters should be an array'));
39
        }
40
41
        $hostScheme = $this->getViewHelperService()->getContext()->getRequest()->getSchemeAndHttpHost();
42
43
        return ($includeHostScheme ? $hostScheme : null) . rtrim($baseUrl, '/')
44
        . $this->getRequestMatcher()
45
            ->reverse($actionName, $parameters, $escapeAmp);
46
    }
47
48
    /**
49
     *
50
     * @return RequestMatcher
51
     */
52
    protected function getRequestMatcher()
53
    {
54
        return $this->getViewHelperService()
55
            ->getApplication()
56
            ->getServices()
57
            ->get($this->serviceName);
58
    }
59
}