HandlerProviderWithContainer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 23
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerForMessage() 0 9 2
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\PSR;
13
14
use ICanBoogie\MessageBus\HandlerProvider;
15
use Psr\Container\ContainerInterface;
16
17
final class HandlerProviderWithContainer implements HandlerProvider
18
{
19
    /**
20
     * @param array<class-string, string> $messageToHandler
21
     *     Where _key_ is a message class and _value_ the service identifier of its handler.
22
     */
23
    public function __construct(
24
        private ContainerInterface $container,
25
        private array $messageToHandler
26
    ) {
27
        $this->container = $container;
28
        $this->messageToHandler = $messageToHandler;
29
    }
30
31
    public function getHandlerForMessage(object $message): ?callable
32
    {
33
        $id = $this->messageToHandler[$message::class] ?? null;
34
35
        if (!$id) {
36
            return null;
37
        }
38
39
        return $this->container->get($id); // @phpstan-ignore-line
40
    }
41
}
42