BusChain.php$0 ➔ dummyBus()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A BusChain.php$0 ➔ dispatch() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus;
5
6
use InvalidArgumentException;
7
8
class BusChain implements Bus
9
{
10
    /**
11
     * @var Chain[]
12
     */
13
    protected $buses = [];
14
15
    public function __construct(Chain ...$buses)
16
    {
17
        if (empty($buses)) {
18
            throw new InvalidArgumentException('Bus Chain cannot be empty.');
19
        }
20
21
        $this->buses = $buses;
22
        $count = count($buses);
23
        for ($i = 0; $i < $count; $i++) {
24
            $nextBus = $this->buses[$i + 1] ?? $this->dummyBus();
25
            $this->buses[$i]->setNext($nextBus);
26
        }
27
    }
28
29
    protected function dummyBus(): Bus
30
    {
31
        return new class implements Bus {
32
            public function dispatch(Message $message)
33
            {
34
            }
35
        };
36
    }
37
38
    public function dispatch(Message $message)
39
    {
40
        return $this->buses[0]->dispatch($message);
41
    }
42
}
43