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

AssertingDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
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