Completed
Push — master ( 3e3d3c...053aa5 )
by Olivier
01:44
created

AssertingDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Dispatcher
21
     */
22
    private $dispatcher;
23
24
    /**
25
     * @var callable
26
     */
27
    private $assertion;
28
29
    /**
30
     * @param Dispatcher $dispatcher
31
     * @param callable $assertion A callable that should throw an exception if the message shouldn't
32
     * be dispatched.
33
     */
34
    public function __construct(Dispatcher $dispatcher, callable $assertion)
35
    {
36
        $this->dispatcher = $dispatcher;
37
        $this->assertion = $assertion;
38
    }
39
40
    /**
41
     * @param object $message
42
     *
43
     * @return mixed
44
     */
45
    public function dispatch(object $message)
46
    {
47
        ($this->assertion)($message);
48
49
        return $this->dispatcher->dispatch($message);
50
    }
51
}
52