Completed
Push — master ( a378d5...7b6893 )
by Flo
28s
created

Route::__invoke()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 9
nop 3
1
<?php
2
/**
3
 * Class Route | Route.php
4
 * @package Faulancer\View\Helper
5
 * @author  Florian Knapp <[email protected]>
6
 */
7
namespace Faulancer\View\Helper;
8
9
use Faulancer\Exception\ConfigInvalidException;
10
use Faulancer\Exception\FactoryMayIncompatibleException;
11
use Faulancer\Exception\ServiceNotFoundException;
12
use Faulancer\Service\Config;
13
use Faulancer\ServiceLocator\ServiceLocator;
14
use Faulancer\View\AbstractViewHelper;
15
use Faulancer\View\ViewController;
16
17
/**
18
 * Class Route
19
 */
20
class Route extends AbstractViewHelper
21
{
22
23
    /**
24
     * Get route path by name
25
     *
26
     * @param ViewController $view
27
     * @param string         $name
28
     * @param array          $parameters
29
     * @return string
30
     * @throws \Exception
31
     */
32
    public function __invoke(ViewController $view, string $name, array $parameters = [])
33
    {
34
        /** @var Config $config */
35
        $config = ServiceLocator::instance()->get(Config::class);
36
        $routes = require $config->get('routeFile');
37
        $path   = '';
38
39
        foreach ($routes as $routeName => $data) {
40
41
            if ($routeName === $name) {
42
                $path = preg_replace('|/\((.*)\)|', '', $data['path']);;
43
                break;
44
            }
45
46
        }
47
48
        if (empty($path)) {
49
            throw new \Exception('No route for name "' . $name . '" found');
50
        }
51
52
        if (!empty($parameters)) {
53
            $path = $path . '/' . implode('/', $parameters);
54
        }
55
56
        return $path;
57
    }
58
59
}