Completed
Push — 8.2 ( 737f16...22bfc7 )
by David
17:32
created

NotFoundRouter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 5
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 14 2
1
<?php
2
3
namespace Mouf\Mvc\Splash\Routers;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7
use Mouf\Mvc\Splash\Controllers\Http404HandlerInterface;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Psr\Log\LoggerInterface;
11
use Mouf\Mvc\Splash\Services\SplashUtils;
12
13
/**
14
 * This router always returns a 404 page, based on the configured page not found controller.
15
 *
16
 * @author Kevin Nguyen
17
 * @author David Négrier
18
 */
19
class NotFoundRouter implements MiddlewareInterface
20
{
21
    /**
22
     * The logger.
23
     *
24
     * @var LoggerInterface
25
     */
26
    private $log;
27
28
    /**
29
     * @var Http404HandlerInterface
30
     */
31
    private $pageNotFoundController;
32
33
    public function __construct(Http404HandlerInterface $pageNotFoundController, LoggerInterface $log = null)
34
    {
35
        $this->pageNotFoundController = $pageNotFoundController;
36
        $this->log = $log;
37
    }
38
39
    /**
40
     * Process an incoming server request and return a response, optionally delegating
41
     * to the next middleware component to create the response.
42
     *
43
     * @param Request $request
44
     * @param DelegateInterface $delegate
45
     *
46
     * @return Response
47
     */
48
    public function process(Request $request, DelegateInterface $delegate)
49
    {
50
        if ($this->log) {
51
            $this->log->info('404 - Page not found on URL: '.$request->getUri()->getPath());
52
        }
53
54
        $response = SplashUtils::buildControllerResponse(
55
            function () use ($request) {
56
                return $this->pageNotFoundController->pageNotFound($request);
57
            }
58
        );
59
60
        return $response;
61
    }
62
}
63