Completed
Push — master ( f9dcdb...5d2770 )
by Enrico
02:49
created

ControllerResolver::getController()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace uSILEX;
3
4
/*
5
 * inspired to https://api.symfony.com/4.1/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.html
6
 */
7
8
use uSILEX\Exception\NotFoundHttpException;
9
10
11
Class ControllerResolver implements ControllerResolverInterface
12
{
13
    protected $app;
14
    
15
    
16 5
    public function __construct(Application $app) 
17
    {
18 5
        assert(isset($app['RouteMatcher']));
19
        
20 5
        $this->app = $app;
21 5
    }
22
23 5
    public function getController() : Route
24
    {
25 5
        assert(isset($this->app['request']));
26
        
27 5
        foreach ($this->app->getRoutes() as $route) {
28 4
            if ($matches = $this->app['RouteMatcher']->match($route)) {
29 3
                $this->app['request.matches'] = $matches;
30 3
                $this->app['request.route'] = $route;
31 4
                return $route;
32
            }
33
        }
34
        
35 2
        throw new NotFoundHttpException('Resource controller not found');
36
    }
37
}