Failed Conditions
Pull Request — master (#1)
by Yo
02:13
created

ContainerJsonRpcMethodResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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 ContainerJsonRpcMethodResolver
12
 */
13
class ContainerJsonRpcMethodResolver implements MethodResolverInterface
14
{
15
    /** @var ContainerInterface */
16
    private $container;
17
    /** @var ServiceNameResolverInterface */
18
    private $serviceNameResolver;
19
20
    public function __construct(
21
        ContainerInterface $container,
22
        ServiceNameResolverInterface $serviceNameResolver
23
    ) {
24
        $this->container = $container;
25
        $this->serviceNameResolver = $serviceNameResolver;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function resolve(string $methodName) : JsonRpcMethodInterface
32
    {
33
        $serviceName = $this->serviceNameResolver->resolve($methodName);
34
        if (!$this->container->has($serviceName)) {
35
            throw new JsonRpcMethodNotFoundException($methodName);
36
        }
37
38
        return $this->container->get($serviceName);
39
    }
40
}
41