Passed
Push — master ( a7b3c3...1a5472 )
by Koldo
02:42 queued 12s
created

ContextFactory::createRedisContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 23
ccs 17
cts 17
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Queue\Container;
6
7
use Antidot\Queue\Container\Config\ConfigProvider;
8
use Assert\Assertion;
9
use Enqueue\Dbal\DbalContext;
10
use Enqueue\Fs\FsConnectionFactory;
11
use Enqueue\Null\NullContext;
12
use Enqueue\Redis\RedisConnectionFactory;
13
use Interop\Queue\Context;
14
use InvalidArgumentException;
15
use Psr\Container\ContainerInterface;
16
17
use function array_key_exists;
18
use function sprintf;
19
20
class ContextFactory
21
{
22
    private const NULL = 'null';
23
    private const FILESYSTEM = 'fs';
24
    private const DBAL = 'dbal';
25
    private const REDIS = 'redis';
26
27 5
    public function __invoke(
28
        ContainerInterface $container,
29
        string $contextName = ConfigProvider::DEFAULT_CONTEXT
30
    ): Context {
31 5
        $contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
32
33 5
        $contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY];
34 5
        if (self::NULL === $contextType) {
35 1
            return new NullContext();
36
        }
37
38 4
        Assertion::keyExists($contextConfig, 'context_params');
39 3
        if (self::FILESYSTEM === $contextType) {
40 1
            return $this->createFilesystemContext($contextConfig['context_params']);
41
        }
42
43 2
        if (self::DBAL === $contextType) {
44 1
            return $this->createDBALContext($container, $contextConfig['context_params']);
45
        }
46
47 1
        if (self::REDIS === $contextType) {
48 1
            return $this->createRedisContext($contextConfig['context_params']);
49
        }
50
51
        throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType));
52
    }
53
54 1
    private function createFilesystemContext(array $contextConfig): Context
55
    {
56 1
        Assertion::classExists(
57 1
            FsConnectionFactory::class,
58 1
            'Install "enqueue/fs" package to run filesystem context.'
59
        );
60 1
        Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.');
61
62 1
        return (new FsConnectionFactory($contextConfig['path']))->createContext();
63
    }
64
65 1
    private function createDBALContext(ContainerInterface $container, array $contextConfig): Context
66
    {
67 1
        Assertion::classExists(
68 1
            DbalContext::class,
69 1
            'Install "enqueue/dbal" package to run filesystem context.'
70
        );
71 1
        Assertion::keyExists(
72 1
            $contextConfig,
73 1
            'connection',
74 1
            'The "connection" service name is required to run dbal context.'
75
        );
76 1
        $context = new DbalContext($container->get($contextConfig['connection']));
77 1
        $context->createDataBaseTable();
78
79 1
        return $context;
80
    }
81
82 1
    private function createRedisContext(array $contextConfig): Context
83
    {
84 1
        Assertion::classExists(
85 1
            RedisConnectionFactory::class,
86 1
            'Install "enqueue/redis" package to run filesystem context.'
87
        );
88 1
        Assertion::keyExists(
89 1
            $contextConfig,
90 1
            'host',
91 1
            'The "host" name is required to run redis context.'
92
        );
93 1
        Assertion::keyExists(
94 1
            $contextConfig,
95 1
            'port',
96 1
            'The "port" is required to run redis context.'
97
        );
98 1
        Assertion::keyExists(
99 1
            $contextConfig,
100 1
            'scheme_extensions',
101 1
            'The "scheme_extensions" name is required to run redis context.'
102
        );
103
104 1
        return (new RedisConnectionFactory($contextConfig))->createContext();
105
    }
106
}
107