ChainTrait::next()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus;
5
6
use RuntimeException;
7
8
trait ChainTrait
9
{
10
    /**
11
     * @var Bus|null
12
     */
13
    protected $nextBus = null;
14
15
    public function setNext(Bus $bus)
16
    {
17
        $this->nextBus = $bus;
18
    }
19
20
    /**
21
     * @param Message $message
22
     * @return mixed
23
     * @throws RuntimeException
24
     */
25
    protected function next(Message $message)
26
    {
27
        if (!$this->hasNext()) {
28
            throw new RuntimeException(
29
                sprintf("[%s] The next Bus was not defined in the chain.", get_class($this))
30
            );
31
        }
32
33
        return $this->nextBus->dispatch($message);
34
    }
35
36
    protected function hasNext(): bool
37
    {
38
        return $this->nextBus instanceof Bus;
39
    }
40
}
41