Passed
Pull Request — master (#1104)
by Maxim
12:14
created

ContainerRegistry   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 31
ccs 10
cts 13
cp 0.7692
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getHandler() 0 13 3
A className() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue;
6
7
use Doctrine\Inflector\Inflector;
8
use Doctrine\Inflector\Rules\English\InflectorFactory;
9
use Psr\Container\ContainerInterface;
10
use Spiral\Core\Exception\Container\ContainerException;
11
use Spiral\Queue\Exception\JobException;
12
13
final class ContainerRegistry implements HandlerRegistryInterface
14
{
15
    private readonly Inflector $inflector;
16
17 357
    public function __construct(
18
        private readonly ContainerInterface $container
19
    ) {
20 357
        $this->inflector = (new InflectorFactory())->build();
0 ignored issues
show
Bug introduced by
The property inflector is declared read-only in Spiral\Queue\ContainerRegistry.
Loading history...
21
    }
22
23 1
    public function getHandler(string $jobType): HandlerInterface
24
    {
25
        try {
26 1
            $handler = $this->container->get($this->className($jobType));
27
        } catch (ContainerException $e) {
28
            throw new JobException($e->getMessage(), $e->getCode(), $e);
29
        }
30
31 1
        if (!$handler instanceof HandlerInterface) {
32
            throw new JobException(\sprintf('Unable to resolve job handler for `%s`', $jobType));
33
        }
34
35 1
        return $handler;
36
    }
37
38 1
    private function className(string $jobType): string
39
    {
40 1
        $names = \explode('.', $jobType);
41 1
        $names = \array_map(fn (string $value) => $this->inflector->classify($value), $names);
42
43 1
        return \implode('\\', $names);
44
    }
45
}
46