Passed
Push — master ( dcedd4...0ea86f )
by Koldo
03:11
created

ContextFactory::__invoke()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 16
nc 7
nop 2
dl 0
loc 33
ccs 17
cts 17
cp 1
crap 7
rs 8.8333
c 1
b 0
f 1
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\Pheanstalk\PheanstalkConnectionFactory;
13
use Enqueue\Redis\RedisConnectionFactory;
14
use Enqueue\Sqs\SqsConnectionFactory;
15
use Interop\Queue\Context;
16
use InvalidArgumentException;
17
use Psr\Container\ContainerInterface;
18
19
use function sprintf;
20
21
class ContextFactory
22
{
23
    private const NULL = 'null';
24
    private const FILESYSTEM = 'fs';
25
    private const DBAL = 'dbal';
26
    private const REDIS = 'redis';
27
    private const SQS = 'sqs';
28
    private const BEANSTALK = 'beanstalk';
29
30 7
    public function __invoke(
31
        ContainerInterface $container,
32
        string $contextName = ConfigProvider::DEFAULT_CONTEXT
33
    ): Context {
34 7
        $contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY));
35
36 7
        $contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY];
37 7
        if (self::NULL === $contextType) {
38 1
            return new NullContext();
39
        }
40
41 6
        Assertion::keyExists($contextConfig, 'context_params');
42 6
        if (self::FILESYSTEM === $contextType) {
43 1
            return $this->createFilesystemContext($contextConfig['context_params']);
44
        }
45
46 5
        if (self::DBAL === $contextType) {
47 1
            return $this->createDBALContext($container, $contextConfig['context_params']);
48
        }
49
50 4
        if (self::REDIS === $contextType) {
51 1
            return $this->createRedisContext($contextConfig['context_params']);
52
        }
53
54 3
        if (self::BEANSTALK === $contextType) {
55 1
            return $this->createBeanstalkContext($contextConfig['context_params']);
56
        }
57
58 2
        if (self::SQS === $contextType) {
59 1
            return $this->createSQSContext($contextConfig['context_params']);
60
        }
61
62 1
        throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType));
63
    }
64
65 1
    private function createFilesystemContext(array $contextConfig): Context
66
    {
67 1
        Assertion::classExists(
68 1
            FsConnectionFactory::class,
69 1
            'Install "enqueue/fs" package to run filesystem context.'
70
        );
71 1
        Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.');
72
73 1
        return (new FsConnectionFactory($contextConfig['path']))->createContext();
74
    }
75
76 1
    private function createDBALContext(ContainerInterface $container, array $contextConfig): Context
77
    {
78 1
        Assertion::classExists(
79 1
            DbalContext::class,
80 1
            'Install "enqueue/dbal" package to run Doctrine DBAL context.'
81
        );
82 1
        Assertion::keyExists(
83 1
            $contextConfig,
84 1
            'connection',
85 1
            'The "connection" service name is required to run dbal context.'
86
        );
87 1
        $context = new DbalContext($container->get($contextConfig['connection']));
88 1
        $context->createDataBaseTable();
89
90 1
        return $context;
91
    }
92
93 1
    private function createRedisContext(array $contextConfig): Context
94
    {
95 1
        Assertion::classExists(
96 1
            RedisConnectionFactory::class,
97 1
            'Install "enqueue/redis" package to run redis context.'
98
        );
99 1
        Assertion::keyExists(
100 1
            $contextConfig,
101 1
            'host',
102 1
            'The "host" name is required to run redis context.'
103
        );
104 1
        Assertion::keyExists(
105 1
            $contextConfig,
106 1
            'port',
107 1
            'The "port" is required to run redis context.'
108
        );
109 1
        Assertion::keyExists(
110 1
            $contextConfig,
111 1
            'scheme_extensions',
112 1
            'The "scheme_extensions" name is required to run redis context.'
113
        );
114
115 1
        return (new RedisConnectionFactory($contextConfig))->createContext();
116
    }
117
118 1
    private function createSQSContext(array $contextConfig): Context
119
    {
120 1
        Assertion::classExists(
121 1
            SqsConnectionFactory::class,
122 1
            'Install "enqueue/sqs" package to run Amazon SQS context.'
123
        );
124 1
        Assertion::keyExists(
125 1
            $contextConfig,
126 1
            'key',
127 1
            'The AWS "key" is required to run Amazon SQS context.'
128
        );
129 1
        Assertion::keyExists(
130 1
            $contextConfig,
131 1
            'secret',
132 1
            'The AWS "secret" is required to run Amazon SQS context.'
133
        );
134 1
        Assertion::keyExists(
135 1
            $contextConfig,
136 1
            'region',
137 1
            'The AWS "region" is required to run Amazon SQS context.'
138
        );
139
140 1
        return (new SqsConnectionFactory($contextConfig))->createContext();
141
    }
142
143 1
    private function createBeanstalkContext(array $contextConfig): Context
144
    {
145 1
        Assertion::classExists(
146 1
            RedisConnectionFactory::class,
147 1
            'Install "enqueue/pheanstalk" package to run beanstalk context.'
148
        );
149 1
        Assertion::keyExists(
150 1
            $contextConfig,
151 1
            'host',
152 1
            'The "host" name is required to run beanstalk context.'
153
        );
154 1
        Assertion::keyExists(
155 1
            $contextConfig,
156 1
            'port',
157 1
            'The "port" is required to run beanstalk context.'
158
        );
159
160 1
        return (new PheanstalkConnectionFactory($contextConfig))->createContext();
161
    }
162
}
163