MapByStaticList   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A mapCommandToHandler() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Tactician\Handler\Mapping;
6
7
use function array_key_exists;
8
9
/**
10
 * This mapping is useful when you're working with a compiled list or a legacy
11
 * app without a consistent naming convention.
12
 *
13
 * The mapping array should be in the following format:
14
 *
15
 *      [
16
 *          SomeCommand::class => [SomeHandler::class, 'handle'],
17
 *          OtherCommand::class => [WhateverHandler::class, 'handleOtherCommand'],
18
 *          ...
19
 *      ]
20
 */
21
final class MapByStaticList implements CommandToHandlerMapping
22
{
23
    /** @var array<string, array<string>> */
24
    private array $mapping;
25
26
    /** @param array<string, array<string>> $mapping */
27 2
    public function __construct(array $mapping)
28
    {
29 2
        $this->mapping = $mapping;
30 2
    }
31
32 2
    public function mapCommandToHandler(string $commandFQCN): MethodToCall
33
    {
34 2
        if (! array_key_exists($commandFQCN, $this->mapping)) {
35 1
            throw FailedToMapCommand::className($commandFQCN);
36
        }
37
38 1
        return new MethodToCall(
39 1
            $this->mapping[$commandFQCN][0],
40 1
            $this->mapping[$commandFQCN][1]
41
        );
42
    }
43
}
44