Completed
Push — master ( d41cf1...36fce9 )
by Javi
23:31
created

RequestHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handlerAttribute() 0 6 1
A arguments() 0 6 1
A process() 0 11 1
1
<?php
2
3
namespace Philae\Middlewares;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Middlewares\Utils\CallableHandler;
7
use Middlewares\Utils\CallableResolver\CallableResolverInterface;
8
use Philae\DefaultServiceProvider;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
class RequestHandler extends AbstractMiddleware
13
{
14
    /**
15
     * @var string Attribute name for handler reference
16
     */
17
    protected $handlerAttribute = 'request-handler';
18
19
    /**
20
     * @var array Extra arguments passed to the handler
21
     */
22
    protected $arguments = [];
23
24
    /**
25
     * Set the attribute name to store handler reference.
26
     *
27
     * @param string $handlerAttribute
28
     *
29
     * @return $this
30
     */
31
    public function handlerAttribute(string $handlerAttribute)
32
    {
33
        $this->handlerAttribute = $handlerAttribute;
34
35
        return $this;
36
    }
37
38
    /**
39
     * Extra arguments passed to the handler.
40
     *
41
     * @return $this
42
     */
43
    public function arguments(...$args)
44
    {
45
        $this->arguments = $args;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Process a server request and return a response.
52
     *
53
     * @param ServerRequestInterface $request
54
     * @param DelegateInterface $delegate
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
59
    {
60
        $arguments = array_merge([$request], $this->arguments);
61
62
        $handler = $request->getAttribute($this->handlerAttribute);
63
        /** @var CallableResolverInterface $resolver */
64
        $resolver = $this->getContainer()->get(DefaultServiceProvider::SERVICE_CALLABLE_RESOLVER);
65
        $callable = $resolver->resolve($handler, $arguments);
66
67
        return CallableHandler::execute($callable, $arguments);
68
    }
69
}
70