Completed
Pull Request — release/3.0.0 (#9)
by Yo
03:08 queued 01:13
created

resolveMethodNameToServiceId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
namespace Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver;
3
4
use Psr\Container\ContainerInterface;
5
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
6
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface;
7
8
/**
9
 * Class ContainerMethodResolver
10
 */
11
class ContainerMethodResolver implements JsonRpcMethodResolverInterface
12
{
13
    /** @var ContainerInterface */
14
    private $container;
15
    /** @var string[] */
16
    private $methodMappingList = [];
17
18
    /**
19
     * @param ContainerInterface $container
20
     */
21 3
    public function __construct(ContainerInterface $container)
22
    {
23 3
        $this->container = $container;
24 3
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 3
    public function resolve(string $methodName) : ?JsonRpcMethodInterface
30
    {
31 3
        $serviceName = $this->resolveMethodNameToServiceId($methodName);
32
33 3
        return $this->container->has($serviceName)
34 2
            ? $this->container->get($serviceName)
35 3
            : null
36
        ;
37
    }
38
39
    /**
40
     * @param string $methodName
41
     * @param string $containerServiceId
42
     */
43 1
    public function addJsonRpcMethodMapping(string $methodName, string $containerServiceId) : void
44
    {
45 1
        $this->methodMappingList[$methodName] = $containerServiceId;
46 1
    }
47
48
    /**
49
     * @param string $methodName
50
     *
51
     * @return string Method's identifier (returns original method name if no mapping defined)
52
     */
53 3
    protected function resolveMethodNameToServiceId(string $methodName) : string
54
    {
55 3
        return isset($this->methodMappingList[$methodName])
56 1
            ? $this->methodMappingList[$methodName]
57 3
            : $methodName
58
        ;
59
    }
60
}
61