MapByStaticList::mapCommandToHandler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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