Completed
Push — master ( 6d9e57...1481a4 )
by Dan
02:55
created

ClosureHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 3 1
A __construct() 0 5 1
A getResponse() 0 7 2
A resolve() 0 4 1
1
<?php
2
3
namespace Ds\Router\Handler;
4
5
use Ds\Router\RouteHandlerInterface;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Closure Route Handler.
10
 * @package Ds\Router\Handler
11
 */
12
class ClosureHandler extends Handler implements RouteHandlerInterface
13
{
14
15
    protected $preResponse;
16
    protected $closure;
17
    protected $resolved = null;
18
19
    /**
20
     * @param string $preResponse
21
     * @param callable $closure
22
     * @return Closure
23
     */
24
    public static function add($preResponse = '', callable $closure){
25
        return new Closure($preResponse, $closure);
26
    }
27
28
    /**
29
     * Closure constructor.
30
     * @param string $preResponse
31
     * @param callable $closure
32
     */
33
    public function __construct($preResponse = '', callable $closure)
34
    {
35
        $this->preResponse = $preResponse;
36
        $this->closure = $closure;
37
    }
38
39
    /**
40
     * @param array $vars
41
     * @return mixed
42
     * @throws \Exception
43
     */
44
    function getResponse($vars = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
        if ($this->resolved === null){
47
            throw new \Exception("Handler must be resolved before calling getContents().");
48
        }
49
        return call_user_func($this->closure,$vars);
50
    }
51
52
    /**
53
     * @param array ...$arguments
54
     */
55
    function resolve(...$arguments)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
56
    {
57
        $this->resolved = true;
58
    }
59
}
60