Passed
Pull Request — master (#592)
by Aleksei
05:28
created

QueueInjector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Queue\Core;
6
7
use ReflectionClass;
8
use Spiral\Core\Container\InjectorInterface;
9
use Spiral\Queue\Exception\InvalidArgumentException;
10
use Spiral\Queue\QueueConnectionProviderInterface;
11
use Spiral\Queue\QueueInterface;
12
13
class QueueInjector implements InjectorInterface
14
{
15
    private QueueConnectionProviderInterface $queueManager;
16
17
    public function __construct(QueueConnectionProviderInterface $queueManager)
18
    {
19
        $this->queueManager = $queueManager;
20
    }
21
22
    public function createInjection(ReflectionClass $class, string $context = null): QueueInterface
23
    {
24
        // Get Queue by context
25
        if ($context === null) {
26
            $connection = $this->queueManager->getConnection();
27
        } else {
28
            try {
29
                $connection = $this->queueManager->getConnection($context);
30
            } catch (InvalidArgumentException $e) {
31
                // Case when context doesn't match to configured connections
32
                return $this->queueManager->getConnection();
33
            }
34
        }
35
36
        // User specified a specific class type
37
        $className = $class->getName();
38
        if ($className !== QueueInterface::class && !$connection instanceof $className) {
39
            throw new \RuntimeException(
40
                \sprintf(
41
                    "The queue obtained by the context `%s` doesn't match the type `%s`.",
42
                    $context,
43
                    $className
44
                )
45
            );
46
        }
47
48
        return $connection;
49
    }
50
}
51