Completed
Push — master ( 206c33...9e77c2 )
by Oscar
05:44
created

HandlerTrait::executeHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Psr7Middlewares\Utils;
4
5
use Psr\Http\Message\RequestInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Utilities used by middlewares with handler options.
10
 */
11
trait HandlerTrait
12
{
13
    use CallableTrait;
14
15
    /**
16
     * @var callable|string|null The handler used
17
     */
18
    protected $handler;
19
20
    /**
21
     * Constructor.
22
     *
23
     * @param callable|string|null $handler
24
     */
25
    public function __construct($handler = null)
26
    {
27
        if ($handler !== null) {
28
            $this->handler($handler);
29
        }
30
    }
31
32
    /**
33
     * Set the handler.
34
     *
35
     * @param string|callable $handler
36
     *
37
     * @return self
38
     */
39
    public function handler($handler)
40
    {
41
        $this->handler = $handler;
42
43
        return $this;
44
    }
45
46
    /**
47
     * Execute the target.
48
     *
49
     * @param RequestInterface  $request
50
     * @param ResponseInterface $response
51
     *
52
     * @return ResponseInterface
53
     */
54
    protected function executeHandler(RequestInterface $request, ResponseInterface $response)
55
    {
56
        return $this->executeCallable($this->handler, $request, $response);
57
    }
58
}
59