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

ContainerMethodResolver::setServiceNameResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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