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

ContainerRegistry::className()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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