Passed
Pull Request — master (#1104)
by Aleksei
26:20
created

QueueManager::resolveConnection()   A

Complexity

Conditions 6
Paths 19

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 18.348

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 28
ccs 6
cts 20
cp 0.3
rs 9.0111
c 0
b 0
f 0
cc 6
nc 19
nop 1
crap 18.348
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Spiral\Core\Container\Autowire;
10
use Spiral\Core\CoreInterceptorInterface as LegacyInterceptor;
11
use Spiral\Core\Exception\Container\ContainerException;
12
use Spiral\Core\FactoryInterface;
13
use Spiral\Core\InterceptorPipeline;
14
use Spiral\Interceptors\InterceptorInterface;
15
use Spiral\Queue\Config\QueueConfig;
16
use Spiral\Queue\Interceptor\Push\Core as PushCore;
17
18
final class QueueManager implements QueueConnectionProviderInterface
19
{
20
    /** @var QueueInterface[] */
21
    private array $pipelines = [];
22
23 8
    public function __construct(
24
        private readonly QueueConfig $config,
25
        private readonly ContainerInterface $container,
26
        private readonly FactoryInterface $factory,
27
        private readonly ?EventDispatcherInterface $dispatcher = null
28
    ) {
29 8
    }
30
31 6
    public function getConnection(?string $name = null): QueueInterface
32
    {
33 6
        $name ??= $this->getDefaultDriver();
34
        // Replaces alias with real pipeline name
35 6
        $name = $this->config->getAliases()[$name] ?? $name;
36
37 6
        if (!isset($this->pipelines[$name])) {
38 6
            $this->pipelines[$name] = $this->resolveConnection($name);
39
        }
40
41 6
        return $this->pipelines[$name];
42
    }
43
44
    /**
45
     * @throws Exception\InvalidArgumentException
46
     * @throws Exception\NotSupportedDriverException
47
     */
48 6
    private function resolveConnection(string $name): QueueInterface
49
    {
50 6
        $config = $this->config->getConnection($name);
51
52
        try {
53 6
            $driver = $this->factory->make($config['driver'], $config);
54 6
            $pipeline = (new InterceptorPipeline($this->dispatcher))->withHandler(new PushCore($driver));
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\InterceptorPipeline has been deprecated: use {@see \Spiral\Interceptors\Handler\InterceptorPipeline} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

54
            $pipeline = (/** @scrutinizer ignore-deprecated */ new InterceptorPipeline($this->dispatcher))->withHandler(new PushCore($driver));
Loading history...
55
56 6
            foreach ($this->config->getPushInterceptors() as $interceptor) {
57
                if (\is_string($interceptor) || $interceptor instanceof Autowire) {
58
                    $interceptor = $this->container->get($interceptor);
0 ignored issues
show
Bug introduced by
It seems like $interceptor can also be of type Spiral\Core\Container\Autowire; however, parameter $id of Psr\Container\ContainerInterface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                    $interceptor = $this->container->get(/** @scrutinizer ignore-type */ $interceptor);
Loading history...
59
                }
60
61
                \assert($interceptor instanceof LegacyInterceptor || $interceptor instanceof InterceptorInterface);
62
                $pipeline->addInterceptor($interceptor);
63
            }
64
65 6
            return new Queue($pipeline);
66
        } catch (ContainerException $e) {
67
            throw new Exception\NotSupportedDriverException(
68
                \sprintf(
69
                    'Driver `%s` is not supported. Connection `%s` cannot be created. Reason: `%s`',
70
                    $config['driver'],
71
                    $name,
72
                    $e->getMessage()
73
                ),
74
                $e->getCode(),
75
                $e
76
            );
77
        }
78
    }
79
80
    /**
81
     * @throws Exception\InvalidArgumentException
82
     */
83 2
    private function getDefaultDriver(): string
84
    {
85 2
        return $this->config->getDefaultDriver();
86
    }
87
}
88