UrlViewHelper   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Importance

Changes 8
Bugs 4 Features 5
Metric Value
wmc 13
c 8
b 4
f 5
cbo 8
dl 0
loc 57
rs 10

3 Methods

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