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
|
|
|
/** |
66
|
|
|
* @param array<mixed> $contextConfig |
67
|
|
|
* @throws \Assert\AssertionFailedException |
68
|
|
|
*/ |
69
|
1 |
|
private function createFilesystemContext(array $contextConfig): Context |
70
|
|
|
{ |
71
|
1 |
|
Assertion::classExists( |
72
|
1 |
|
FsConnectionFactory::class, |
73
|
1 |
|
'Install "enqueue/fs" package to run filesystem context.' |
74
|
|
|
); |
75
|
1 |
|
Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.'); |
76
|
|
|
|
77
|
1 |
|
return (new FsConnectionFactory($contextConfig['path']))->createContext(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array<mixed> $contextConfig |
82
|
|
|
* @throws \Assert\AssertionFailedException |
83
|
|
|
*/ |
84
|
1 |
|
private function createDBALContext(ContainerInterface $container, array $contextConfig): Context |
85
|
|
|
{ |
86
|
1 |
|
Assertion::classExists( |
87
|
1 |
|
DbalContext::class, |
88
|
1 |
|
'Install "enqueue/dbal" package to run Doctrine DBAL context.' |
89
|
|
|
); |
90
|
1 |
|
Assertion::keyExists( |
91
|
1 |
|
$contextConfig, |
92
|
1 |
|
'connection', |
93
|
1 |
|
'The "connection" service name is required to run dbal context.' |
94
|
|
|
); |
95
|
1 |
|
$context = new DbalContext($container->get($contextConfig['connection'])); |
96
|
1 |
|
$context->createDataBaseTable(); |
97
|
|
|
|
98
|
1 |
|
return $context; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array<mixed> $contextConfig |
103
|
|
|
* @throws \Assert\AssertionFailedException |
104
|
|
|
*/ |
105
|
1 |
|
private function createRedisContext(array $contextConfig): Context |
106
|
|
|
{ |
107
|
1 |
|
Assertion::classExists( |
108
|
1 |
|
RedisConnectionFactory::class, |
109
|
1 |
|
'Install "enqueue/redis" package to run redis context.' |
110
|
|
|
); |
111
|
1 |
|
Assertion::keyExists( |
112
|
1 |
|
$contextConfig, |
113
|
1 |
|
'host', |
114
|
1 |
|
'The "host" name is required to run redis context.' |
115
|
|
|
); |
116
|
1 |
|
Assertion::keyExists( |
117
|
1 |
|
$contextConfig, |
118
|
1 |
|
'port', |
119
|
1 |
|
'The "port" is required to run redis context.' |
120
|
|
|
); |
121
|
1 |
|
Assertion::keyExists( |
122
|
1 |
|
$contextConfig, |
123
|
1 |
|
'scheme_extensions', |
124
|
1 |
|
'The "scheme_extensions" name is required to run redis context.' |
125
|
|
|
); |
126
|
|
|
|
127
|
1 |
|
return (new RedisConnectionFactory($contextConfig))->createContext(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array<mixed> $contextConfig |
132
|
|
|
* @throws \Assert\AssertionFailedException |
133
|
|
|
*/ |
134
|
1 |
|
private function createSQSContext(array $contextConfig): Context |
135
|
|
|
{ |
136
|
1 |
|
Assertion::classExists( |
137
|
1 |
|
SqsConnectionFactory::class, |
138
|
1 |
|
'Install "enqueue/sqs" package to run Amazon SQS context.' |
139
|
|
|
); |
140
|
1 |
|
Assertion::keyExists( |
141
|
1 |
|
$contextConfig, |
142
|
1 |
|
'key', |
143
|
1 |
|
'The AWS "key" is required to run Amazon SQS context.' |
144
|
|
|
); |
145
|
1 |
|
Assertion::keyExists( |
146
|
1 |
|
$contextConfig, |
147
|
1 |
|
'secret', |
148
|
1 |
|
'The AWS "secret" is required to run Amazon SQS context.' |
149
|
|
|
); |
150
|
1 |
|
Assertion::keyExists( |
151
|
1 |
|
$contextConfig, |
152
|
1 |
|
'region', |
153
|
1 |
|
'The AWS "region" is required to run Amazon SQS context.' |
154
|
|
|
); |
155
|
|
|
|
156
|
1 |
|
return (new SqsConnectionFactory($contextConfig))->createContext(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array<mixed> $contextConfig |
161
|
|
|
* @throws \Assert\AssertionFailedException |
162
|
|
|
*/ |
163
|
1 |
|
private function createBeanstalkContext(array $contextConfig): Context |
164
|
|
|
{ |
165
|
1 |
|
Assertion::classExists( |
166
|
1 |
|
RedisConnectionFactory::class, |
167
|
1 |
|
'Install "enqueue/pheanstalk" package to run beanstalk context.' |
168
|
|
|
); |
169
|
1 |
|
Assertion::keyExists( |
170
|
1 |
|
$contextConfig, |
171
|
1 |
|
'host', |
172
|
1 |
|
'The "host" name is required to run beanstalk context.' |
173
|
|
|
); |
174
|
1 |
|
Assertion::keyExists( |
175
|
1 |
|
$contextConfig, |
176
|
1 |
|
'port', |
177
|
1 |
|
'The "port" is required to run beanstalk context.' |
178
|
|
|
); |
179
|
|
|
|
180
|
1 |
|
return (new PheanstalkConnectionFactory($contextConfig))->createContext(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|