RoutingHelper::handle()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.5222
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 5.0342
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace JaySDe\HandlebarsBundle\Helper;
7
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
10
class RoutingHelper implements HelperInterface
11
{
12
    private $generator;
13
    private $type;
14
15 4
    public function __construct(UrlGeneratorInterface $generator, $type)
16
    {
17 4
        $this->generator = $generator;
18 4
        $this->type = strtolower($type);
19 4
    }
20
    
21 4
    public function handle($context, $options)
22
    {
23 4
        $options = isset($options['hash']) ? $options['hash'] : [];
24 4
        $relative = isset($options['relative']) ? $options['relative'] : false;
25 4
        switch ($this->type) {
26 4
            case 'path':
27 2
                return $this->getPath($context, $options, $relative);
28 2
            case 'url':
29 2
                return $this->getUrl($context, $options, $relative);
30
            default:
31
                throw new \InvalidArgumentException();
32
        }
33
    }
34
35 2
    private function getPath($name, $parameters = array(), $relative = false)
36
    {
37 2
        return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
38
    }
39
40 2
    private function getUrl($name, $parameters = array(), $schemeRelative = false)
41
    {
42 2
        return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
43
    }
44
}
45