Passed
Push — develop ( 56845b...9da81a )
by Michele
46s queued 11s
created

Listener::setParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zanzara\Listener;
6
7
use Psr\Container\ContainerInterface;
8
use Zanzara\Context;
9
use Zanzara\Middleware\MiddlewareCollector;
10
use Zanzara\Middleware\MiddlewareInterface;
11
use Zanzara\Middleware\MiddlewareNode;
12
13
/**
14
 * Each listener has a middleware chain.
15
 * On listener instantiation the object itself is set as tip of the middleware stack.
16
 *
17
 */
18
class Listener extends MiddlewareCollector implements MiddlewareInterface
19
{
20
21
    /**
22
     * @var string|null
23
     */
24
    protected $id;
25
26
    /**
27
     * @var callable
28
     */
29
    protected $callback;
30
31
    /**
32
     * @var array
33
     */
34
    protected $parameters = [];
35
36
    /**
37
     * @param  $callback
38
     * @param ContainerInterface $container
39
     * @param string|null $id
40
     * @throws \DI\DependencyException
41
     * @throws \DI\NotFoundException
42
     */
43
    public function __construct($callback, ContainerInterface $container, ?string $id = null)
44
    {
45
        parent::__construct($container);
46
        $this->id = $id;
47
        $this->callback = $this->getCallable($callback);
48
        $this->tip = new MiddlewareNode($this);
49
    }
50
51
    /**
52
     * @inheritDoc
53
     */
54
    public function handle(Context $ctx, $next)
55
    {
56
        call_user_func($this->callback, $ctx, ...array_merge($this->parameters, [$next]));
57
    }
58
59
    /**
60
     * @return MiddlewareNode
61
     */
62
    public function getTip(): MiddlewareNode
63
    {
64
        return $this->tip;
65
    }
66
67
    /**
68
     * @param array $parameters
69
     * @return Listener
70
     */
71
    public function setParameters(array $parameters)
72
    {
73
        $this->parameters = $parameters;
74
        return $this;
75
    }
76
77
}
78