RouterRule::getRouter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dazzle\Channel\Router;
4
5
use Dazzle\Channel\Protocol\ProtocolInterface;
6
7
class RouterRule
8
{
9
    /**
10
     * @var RouterInterface
11
     */
12
    protected $router;
13
14
    /**
15
     * @var callable
16
     */
17
    protected $matcher;
18
19
    /**
20
     * @var callable
21
     */
22
    protected $handler;
23
24
    /**
25
     * @var bool
26
     */
27
    protected $propagate;
28
29
    /**
30
     * @var int
31
     */
32
    protected $limit;
33
34
    /**
35
     * @var array|null
36
     */
37
    protected $pointer;
38
39
    /**
40
     * @var bool
41
     */
42
    protected $cancelled;
43
44
    /**
45
     * @param RouterInterface $router
46
     * @param callable $matcher
47
     * @param callable $handler
48
     * @param bool $propagate
49
     * @param int $limit
50
     */
51 24
    public function __construct(RouterInterface $router, callable $matcher, callable $handler, $propagate = false, $limit = 0)
52
    {
53 24
        $this->router = $router;
54 24
        $this->matcher = $matcher;
55 24
        $this->handler = $handler;
56 24
        $this->propagate = $propagate;
57 24
        $this->limit = $limit;
58 24
        $this->pointer = null;
59 24
        $this->cancelled = false;
60 24
    }
61
62
    /**
63
     *
64
     */
65 10
    public function __destruct()
66
    {
67 10
        unset($this->router);
68 10
        unset($this->matcher);
69 10
        unset($this->handler);
70 10
        unset($this->propagate);
71 10
        unset($this->pointer);
72 10
        unset($this->cancelled);
73 10
    }
74
75
    /**
76
     * Return Router to which handler is attached to.
77
     *
78
     * @return RouterInterface
79
     */
80 1
    public function getRouter()
81
    {
82 1
        return $this->router;
83
    }
84
85
    /**
86
     * Match given message protocol.
87
     *
88
     * @param string $sender
89
     * @param ProtocolInterface $protocol
90
     * @return bool
91
     */
92 8
    public function match($sender, ProtocolInterface $protocol)
93
    {
94 8
        $callback = $this->matcher;
95 8
        return $callback($sender, $protocol);
96
    }
97
98
    /**
99
     * Handle given message protocol.
100
     *
101
     * @param string $sender
102
     * @param ProtocolInterface $protocol
103
     * @param int $flags
104
     * @param callable|null $success
105
     * @param callable|null $failure
106
     * @param callable|null $cancel
107
     * @param float $timeout
108
     * @return bool
109
     */
110 12
    public function handle($sender, ProtocolInterface $protocol, $flags, callable $success = null, callable $failure = null, callable $cancel = null, $timeout = 0.0)
111
    {
112 12
        $callback = $this->handler;
113 12
        $callback($sender, $protocol, $flags, $success, $failure, $cancel, $timeout);
114
115 12
        if ($this->limit > 0)
116
        {
117 2
            $this->limit--;
118 2
            if ($this->limit === 0)
119
            {
120 1
                $this->cancel();
121
            }
122
        }
123
124 12
        return $this->propagate;
125
    }
126
127
    /**
128
     * Remove this handler.
129
     */
130
    public function cancel()
131
    {
132
        if (isset($this->pointer) && isset($this->router) && !$this->cancelled)
133
        {
134
            $this->router->removeHandler($this->pointer[0], $this->pointer[1]);
0 ignored issues
show
Bug introduced by
The method removeHandler() does not exist on Dazzle\Channel\Router\RouterInterface. Did you maybe mean handle()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
135
            $this->cancelled = true;
136
        }
137
    }
138
139
    /**
140
     * @internal
141
     * @param string $stack
142
     * @param int $pointer
143
     */
144 16
    public function setPointer($stack, $pointer)
145
    {
146 16
        $this->pointer = [ $stack, $pointer ];
147 16
    }
148
}
149