Passed
Pull Request — feature/symfony-config (#2)
by Yo
01:56
created

ContainerMethodResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setServiceNameResolver() 0 5 1
A __construct() 0 3 1
A resolve() 0 11 3
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|null */
18
    private $serviceNameResolver = null;
19
20
    /**
21
     * @param ContainerInterface $container
22
     */
23 4
    public function __construct(ContainerInterface $container)
24
    {
25 4
        $this->container = $container;
26 4
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 4
    public function resolve(string $methodName) : JsonRpcMethodInterface
32
    {
33 4
        $serviceName = null !== $this->serviceNameResolver
34 2
            ? $this->serviceNameResolver->resolve($methodName)
35 4
            : $methodName
36
        ;
37 4
        if (!$this->container->has($serviceName)) {
38 2
            throw new JsonRpcMethodNotFoundException($methodName);
39
        }
40
41 2
        return $this->container->get($serviceName);
42
    }
43
44
    /**
45
     * @param ServiceNameResolverInterface $serviceNameResolver
46
     *
47
     * @return ContainerMethodResolver
48
     */
49 2
    public function setServiceNameResolver(ServiceNameResolverInterface $serviceNameResolver) : ContainerMethodResolver
50
    {
51 2
        $this->serviceNameResolver = $serviceNameResolver;
52
53 2
        return $this;
54
    }
55
}
56