ChainTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 8
c 0
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 9 2
A hasNext() 0 3 1
A setNext() 0 3 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