UrlViewHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
c 3
b 0
f 3
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
}