UrlViewHelper::execute()   D
last analyzed

Complexity

Conditions 11
Paths 128

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 3 Features 5
Metric Value
c 7
b 3
f 5
dl 0
loc 34
rs 4.9629
cc 11
eloc 21
nc 128
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}