Test Failed
Pull Request — master (#19)
by Flo
04:39
created

Link::__invoke()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 43
Code Lines 21

Duplication

Lines 9
Ratio 20.93 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 9
loc 43
rs 8.439
cc 6
eloc 21
nc 17
nop 3
1
<?php
2
/**
3
 * Class Link | Link.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\Exception\RouteInvalidException;
10
use Faulancer\Http\Request;
11
use Faulancer\Service\Config;
12
use Faulancer\Service\RequestService;
13
use Faulancer\View\AbstractViewHelper;
14
use Faulancer\View\ViewController;
15
16
/**
17
 * Class Link
18
 */
19
class Link extends AbstractViewHelper
20
{
21
22
    /**
23
     * @param ViewController $view
24
     * @param string         $routeName
25
     * @param array          $elementAttributes
26
     * @return string
27
     * @throws RouteInvalidException
28
     * @codeCoverageIgnore
29
     */
30
    public function __invoke(ViewController $view, $routeName, $elementAttributes = [])
31
    {
32
        $id    = '';
33
        $class = '';
34
        $style = '';
35
36
        $serviceLocator = $this->getServiceLocator();
37
38
        /** @var Config $config */
39
        $config = $serviceLocator->get(Config::class);
40
41
        /** @var Request $request */
42
        $request = $serviceLocator->get(RequestService::class);
43
44
        $route = $config->get('routes:' . $routeName);
45
46
        if (empty($route)) {
47
            throw new RouteInvalidException('No valid route for "' . $routeName . '" found.');
48
        }
49
50
        if ($request->getUri() === $route['path']) {
51
            $elementAttributes['class'][] = 'selected';
52
        }
53
54 View Code Duplication
        if (!empty($elementAttributes['id'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55
            $id = ' id="' . implode(' ', $elementAttributes['id']) . '" ';
56
        }
57
58 View Code Duplication
        if (!empty($elementAttributes['class'])){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $class = 'class="' . implode(' ', $elementAttributes['class']) . '" ';
60
        }
61
62 View Code Duplication
        if (!empty($elementAttributes['style'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $style = 'style="' . implode(' ', $elementAttributes['style']) . '" ';
64
        }
65
66
        $linkPattern = '<a ' . $id . $class . $style . 'href="%s" onfocus="blur()">%s</a>';
67
68
        $link = sprintf($linkPattern, $route['path'], $route['title']);
69
70
        return $link;
71
72
    }
73
74
}