Bus::fromMiddlewareList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Werkspot\MessageBus\Bus;
4
5
use Werkspot\MessageBus\Bus\DeliveryChain\MiddlewareInterface;
6
use Werkspot\MessageBus\Message\MessageInterface;
7
8
final class Bus implements BusInterface
9
{
10
    /**
11
     * @var callable
12
     */
13
    private $deliveryChain;
14
15
    public static function fromMiddlewareList(MiddlewareInterface ...$middlewareList): self
16
    {
17 2
        $deliveryChain = function (): void {
18 2
        };
19
20 2
        while ($middleware = array_pop($middlewareList)) {
21 2
            $deliveryChain = function (MessageInterface $message) use ($middleware, $deliveryChain): void {
22 2
                $middleware->deliver($message, $deliveryChain);
23 2
            };
24
        }
25
26 2
        return new self($deliveryChain);
27
    }
28
29 2
    private function __construct(callable $deliveryChain)
30
    {
31 2
        $this->deliveryChain = $deliveryChain;
32 2
    }
33
34 2
    public function deliver(MessageInterface...$messageList): void
35
    {
36 2
        $chain = $this->deliveryChain;
37 2
        foreach ($messageList as $message) {
38 2
            $chain($message);
39
        }
40 2
    }
41
}
42