Passed
Push — master ( a2340d...9d7d2b )
by butschster
06:32
created

QueueManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 55.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 29
dl 0
loc 72
ccs 19
cts 34
cp 0.5588
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConnection() 0 11 2
A getDefaultDriver() 0 3 1
A __construct() 0 6 1
A resolveConnection() 0 32 5
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;
11
use Spiral\Core\Exception\Container\ContainerException;
12
use Spiral\Core\FactoryInterface;
13
use Spiral\Core\InterceptableCore;
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
22 8
    public function __construct(
23
        private readonly QueueConfig $config,
24
        private readonly ContainerInterface $container,
25
        private readonly FactoryInterface $factory,
26
        private readonly ?EventDispatcherInterface $dispatcher = null
27
    ) {
28 8
    }
29
30 6
    public function getConnection(?string $name = null): QueueInterface
31
    {
32 6
        $name ??= $this->getDefaultDriver();
33
        // Replaces alias with real pipeline name
34 6
        $name = $this->config->getAliases()[$name] ?? $name;
35
36 6
        if (!isset($this->pipelines[$name])) {
37 6
            $this->pipelines[$name] = $this->resolveConnection($name);
38
        }
39
40 6
        return $this->pipelines[$name];
41
    }
42
43
    /**
44
     * @throws Exception\InvalidArgumentException
45
     * @throws Exception\NotSupportedDriverException
46
     */
47 6
    private function resolveConnection(string $name): QueueInterface
48
    {
49 6
        $config = $this->config->getConnection($name);
50
51
        try {
52 6
            $driver = $this->factory->make($config['driver'], $config);
53
54 6
            $core = new InterceptableCore(
55 6
                new PushCore($driver),
56 6
                $this->dispatcher
57 6
            );
58
59 6
            foreach ($this->config->getPushInterceptors() as $interceptor) {
60
                if (\is_string($interceptor) || $interceptor instanceof Autowire) {
61
                    $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

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