Completed
Push — master ( 7dba56...6875ae )
by Oscar
10:21
created

LeagueRoute::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use League\Route\RouteCollection;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use RuntimeException;
9
10
class LeagueRoute
11
{
12
    /**
13
     * @var RouteCollection|null The router container
14
     */
15
    private $router;
16
17
    /**
18
     * Constructor. Set the RouteCollection instance.
19
     *
20
     * @param RouteCollection $router
21
     */
22
    public function __construct(RouteCollection $router = null)
23
    {
24
        if ($router !== null) {
25
            $this->router($router);
26
        }
27
    }
28
29
    /**
30
     * Extra arguments passed to the controller.
31
     *
32
     * @param RouteCollection $router
33
     *
34
     * @return self
35
     */
36
    public function router(RouteCollection $router)
37
    {
38
        $this->router = $router;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Execute the middleware.
45
     *
46
     * @param ServerRequestInterface $request
47
     * @param ResponseInterface      $response
48
     * @param callable               $next
49
     *
50
     * @return ResponseInterface
51
     */
52
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
53
    {
54
        if (empty($this->router)) {
55
            throw new RuntimeException('No RouteCollection instance has been provided');
56
        }
57
58
        return $next($request, $this->router->dispatch($request, $response));
59
    }
60
}
61