Completed
Push — master ( 4e52eb...3a97b2 )
by Oscar
02:26
created

Middleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Folk;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\UriInterface;
8
use Interop\Http\ServerMiddleware\MiddlewareInterface;
9
use Interop\Http\ServerMiddleware\DelegateInterface;
10
11
class Middleware implements MiddlewareInterface
12
{
13
    private $uri;
14
    private $factory;
15
16
    /**
17
     * Set the Uri of the admin.
18
     *
19
     * @param UriInterface $uri
20
     * @param ...$arguments
21
     */
22
    public function __construct(UriInterface $uri, callable $factory)
23
    {
24
        $this->uri = $uri;
25
        $this->factory = $factory;
26
    }
27
28
    /**
29
     * Process a server request and return a response.
30
     *
31
     * @param ServerRequestInterface $request
32
     * @param DelegateInterface      $delegate
33
     *
34
     * @return ResponseInterface
35
     */
36
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
37
    {
38
        if (strpos($request->getUri()->getPath(), $this->uri->getPath()) === 0) {
39
            $admin = call_user_func($this->factory, $this->uri);
40
41
            return $admin($request);
42
        }
43
44
        return $delegate->process($request);
45
    }
46
}
47