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

SimpleHandlerProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHandlerForMessage() 0 11 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 simple implementation of {@link HandlerProvider}.
16
 */
17
final class SimpleHandlerProvider implements HandlerProvider
18
{
19
    /**
20
     * @var array<string, callable>
21
     */
22
    private $handlers;
23
24
    /**
25
     * @param array<string, callable> $handlers
26
     */
27
    public function __construct(array $handlers)
28
    {
29
        $this->handlers = $handlers;
30
    }
31
32
    public function getHandlerForMessage(object $message): callable
33
    {
34
        $class = get_class($message);
35
        $handler = $this->handlers[$class] ?? null;
36
37
        if (!$handler) {
38
            throw new NotFound("No handler for messages of type `$class`.");
39
        }
40
41
        return $handler;
42
    }
43
}
44