Passed
Branch master (aa5120)
by Yo
02:31
created

ContainerMethodResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A resolve() 0 8 2
1
<?php
2
namespace Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver;
3
4
use Psr\Container\ContainerInterface;
5
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
6
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface;
7
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
8
use Yoanm\JsonRpcServerPsr11Resolver\Domain\Model\ServiceNameResolverInterface;
9
10
/**
11
 * Class ContainerMethodResolver
12
 */
13
class ContainerMethodResolver implements MethodResolverInterface
14
{
15
    /** @var ContainerInterface */
16
    private $container;
17
    /** @var ServiceNameResolverInterface */
18
    private $serviceNameResolver;
19
20 2
    public function __construct(
21
        ContainerInterface $container,
22
        ServiceNameResolverInterface $serviceNameResolver
23
    ) {
24 2
        $this->container = $container;
25 2
        $this->serviceNameResolver = $serviceNameResolver;
26 2
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 2
    public function resolve(string $methodName) : JsonRpcMethodInterface
32
    {
33 2
        $serviceName = $this->serviceNameResolver->resolve($methodName);
34 2
        if (!$this->container->has($serviceName)) {
35 1
            throw new JsonRpcMethodNotFoundException($methodName);
36
        }
37
38 1
        return $this->container->get($serviceName);
39
    }
40
}
41