MapByStaticList::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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