Passed
Pull Request — master (#1111)
by Aleksei
11:44
created

QueueManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 53.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
eloc 28
c 1
b 0
f 1
dl 0
loc 68
ccs 16
cts 30
cp 0.5333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 11 2
A getDefaultDriver() 0 3 1
A resolveConnection() 0 25 5
A __construct() 0 8 1
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\CompatiblePipelineBuilder;
10
use Spiral\Core\Container\Autowire;
11
use Spiral\Core\Exception\Container\ContainerException;
12
use Spiral\Core\FactoryInterface;
13
use Spiral\Interceptors\PipelineBuilderInterface;
14
use Spiral\Queue\Config\QueueConfig;
15
use Spiral\Queue\Interceptor\Push\Core as PushCore;
16
17
final class QueueManager implements QueueConnectionProviderInterface
18
{
19
    /** @var QueueInterface[] */
20
    private array $pipelines = [];
21
    private PipelineBuilderInterface $builder;
22
23 8
    public function __construct(
24
        private readonly QueueConfig $config,
25
        private readonly ContainerInterface $container,
26
        private readonly FactoryInterface $factory,
27
        ?EventDispatcherInterface $dispatcher = null,
28
        ?PipelineBuilderInterface $builder = null,
29
    ) {
30 8
        $this->builder = $builder ?? new CompatiblePipelineBuilder($dispatcher);
0 ignored issues
show
Deprecated Code introduced by
The class Spiral\Core\CompatiblePipelineBuilder has been deprecated: Use {@see PipelineBuilder} instead. ( Ignorable by Annotation )

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

30
        $this->builder = $builder ?? /** @scrutinizer ignore-deprecated */ new CompatiblePipelineBuilder($dispatcher);
Loading history...
31
    }
32
33 6
    public function getConnection(?string $name = null): QueueInterface
34
    {
35 6
        $name ??= $this->getDefaultDriver();
36
        // Replaces alias with real pipeline name
37 6
        $name = $this->config->getAliases()[$name] ?? $name;
38
39 6
        if (!isset($this->pipelines[$name])) {
40 6
            $this->pipelines[$name] = $this->resolveConnection($name);
41
        }
42
43 6
        return $this->pipelines[$name];
44
    }
45
46
    /**
47
     * @throws Exception\InvalidArgumentException
48
     * @throws Exception\NotSupportedDriverException
49
     */
50 6
    private function resolveConnection(string $name): QueueInterface
51
    {
52 6
        $config = $this->config->getConnection($name);
53
54
        try {
55 6
            $driver = $this->factory->make($config['driver'], $config);
56
57 6
            $list = [];
58 6
            foreach ($this->config->getPushInterceptors() as $interceptor) {
59
                $list[] = \is_string($interceptor) || $interceptor instanceof Autowire
60
                    ? $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

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