Test Failed
Branch v0.2 (3b8de8)
by Freddie
02:14
created

LoadRoutes   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A loadRoutes() 0 12 1
A __invoke() 0 7 1
A __construct() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Simplex package.
5
 *
6
 * (c) Freddie Frantzen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Simplex\HttpMiddleware;
12
13
use Psr\Container\ContainerInterface;
14
use Psr\Http\Message\ResponseInterface as Response;
15
use Psr\Http\Message\ServerRequestInterface;
16
use Simplex\ContainerKeys;
17
use Simplex\Routing\RouteCollectionBuilder;
18
use Symfony\Component\Routing\Generator\UrlGenerator;
19
use Symfony\Component\Routing\RequestContext;
20
21
class LoadRoutes
22
{
23
    /** @var ContainerInterface */
24
    private $container;
25
26
    public function __construct(ContainerInterface $container)
27
    {
28
        $this->container = $container;
29
    }
30
    
31
    public function __invoke(ServerRequestInterface $request, Response $response, callable $next)
32
    {
33
        $this->loadRoutes();
34
35
        $response = $next($request, $response);
36
37
        return $response;
38
    }
39
40
    private function loadRoutes(): void
41
    {
42
        $builder = $this->container->get(RouteCollectionBuilder::class);
43
44
        $modules = $this->container->get(ContainerKeys::MODULES);
45
46
        $routeCollection = $builder->build($this->container, $modules);
47
48
        $urlGenerator = new UrlGenerator($routeCollection, new RequestContext());
49
50
        $this->container->set(ContainerKeys::ROUTE_COLLECTION, $routeCollection);
0 ignored issues
show
Bug introduced by
The method set() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as DI\Container. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $this->container->/** @scrutinizer ignore-call */ set(ContainerKeys::ROUTE_COLLECTION, $routeCollection);
Loading history...
51
        $this->container->set(UrlGenerator::class, $urlGenerator);
52
    }
53
}
54