Passed
Branch master (893f23)
by Olivier
10:57
created

AssertingDispatcher   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A dispatch() 0 5 1
A __construct() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\MessageBus;
13
14
/**
15
 * A Dispatcher decorator that asserts messages before dispatching them.
16
 */
17
class AssertingDispatcher implements Dispatcher
18
{
19
    /**
20
     * @var callable
21
     */
22
    private $assertion;
23
24
    /**
25
     * @param callable $assertion
26
     *     A callable that should throw an exception if the message shouldn't be dispatched.
27
     */
28
    public function __construct(
29
        private Dispatcher $dispatcher,
30
        callable $assertion
31
    ) {
32
        $this->dispatcher = $dispatcher;
33
        $this->assertion = $assertion;
34
    }
35
36
    /**
37
     * @param object $message
38
     *
39
     * @return mixed
40
     */
41
    public function dispatch(object $message)
42
    {
43
        ($this->assertion)($message);
44
45
        return $this->dispatcher->dispatch($message);
46
    }
47
}
48