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 Enqueue\Sqs\SqsConnectionFactory; |
14
|
|
|
use Interop\Queue\Context; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
|
18
|
|
|
use function array_key_exists; |
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
|
|
|
|
29
|
6 |
|
public function __invoke( |
30
|
|
|
ContainerInterface $container, |
31
|
|
|
string $contextName = ConfigProvider::DEFAULT_CONTEXT |
32
|
|
|
): Context { |
33
|
6 |
|
$contextConfig = ConfigProvider::getContextConfig($contextName, $container->get(ConfigProvider::CONFIG_KEY)); |
34
|
|
|
|
35
|
6 |
|
$contextType = $contextConfig[ConfigProvider::CONTEXTS_TYPE_KEY]; |
36
|
6 |
|
if (self::NULL === $contextType) { |
37
|
1 |
|
return new NullContext(); |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
|
Assertion::keyExists($contextConfig, 'context_params'); |
41
|
5 |
|
if (self::FILESYSTEM === $contextType) { |
42
|
1 |
|
return $this->createFilesystemContext($contextConfig['context_params']); |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
if (self::DBAL === $contextType) { |
46
|
1 |
|
return $this->createDBALContext($container, $contextConfig['context_params']); |
47
|
|
|
} |
48
|
|
|
|
49
|
3 |
|
if (self::REDIS === $contextType) { |
50
|
1 |
|
return $this->createRedisContext($contextConfig['context_params']); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
if (self::SQS === $contextType) { |
54
|
1 |
|
return $this->createSQSContext($contextConfig['context_params']); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
throw new InvalidArgumentException(sprintf('There is not implementation for given context %s.', $contextType)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
private function createFilesystemContext(array $contextConfig): Context |
61
|
|
|
{ |
62
|
1 |
|
Assertion::classExists( |
63
|
1 |
|
FsConnectionFactory::class, |
64
|
1 |
|
'Install "enqueue/fs" package to run filesystem context.' |
65
|
|
|
); |
66
|
1 |
|
Assertion::keyExists($contextConfig, 'path', 'Absolute "path" is required to run filesystem context.'); |
67
|
|
|
|
68
|
1 |
|
return (new FsConnectionFactory($contextConfig['path']))->createContext(); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
private function createDBALContext(ContainerInterface $container, array $contextConfig): Context |
72
|
|
|
{ |
73
|
1 |
|
Assertion::classExists( |
74
|
1 |
|
DbalContext::class, |
75
|
1 |
|
'Install "enqueue/dbal" package to run Doctrine DBAL context.' |
76
|
|
|
); |
77
|
1 |
|
Assertion::keyExists( |
78
|
1 |
|
$contextConfig, |
79
|
1 |
|
'connection', |
80
|
1 |
|
'The "connection" service name is required to run dbal context.' |
81
|
|
|
); |
82
|
1 |
|
$context = new DbalContext($container->get($contextConfig['connection'])); |
83
|
1 |
|
$context->createDataBaseTable(); |
84
|
|
|
|
85
|
1 |
|
return $context; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
private function createRedisContext(array $contextConfig): Context |
89
|
|
|
{ |
90
|
1 |
|
Assertion::classExists( |
91
|
1 |
|
RedisConnectionFactory::class, |
92
|
1 |
|
'Install "enqueue/redis" package to run redis context.' |
93
|
|
|
); |
94
|
1 |
|
Assertion::keyExists( |
95
|
1 |
|
$contextConfig, |
96
|
1 |
|
'host', |
97
|
1 |
|
'The "host" name is required to run redis context.' |
98
|
|
|
); |
99
|
1 |
|
Assertion::keyExists( |
100
|
1 |
|
$contextConfig, |
101
|
1 |
|
'port', |
102
|
1 |
|
'The "port" is required to run redis context.' |
103
|
|
|
); |
104
|
1 |
|
Assertion::keyExists( |
105
|
1 |
|
$contextConfig, |
106
|
1 |
|
'scheme_extensions', |
107
|
1 |
|
'The "scheme_extensions" name is required to run redis context.' |
108
|
|
|
); |
109
|
|
|
|
110
|
1 |
|
return (new RedisConnectionFactory($contextConfig))->createContext(); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
private function createSQSContext($contextConfig): Context |
114
|
|
|
{ |
115
|
1 |
|
Assertion::classExists( |
116
|
1 |
|
SqsConnectionFactory::class, |
117
|
1 |
|
'Install "enqueue/sqs" package to run Amazon SQS context.' |
118
|
|
|
); |
119
|
1 |
|
Assertion::keyExists( |
120
|
1 |
|
$contextConfig, |
121
|
1 |
|
'key', |
122
|
1 |
|
'The AWS "key" is required to run Amazon SQS context.' |
123
|
|
|
); |
124
|
1 |
|
Assertion::keyExists( |
125
|
1 |
|
$contextConfig, |
126
|
1 |
|
'secret', |
127
|
1 |
|
'The AWS "secret" is required to run Amazon SQS context.' |
128
|
|
|
); |
129
|
1 |
|
Assertion::keyExists( |
130
|
1 |
|
$contextConfig, |
131
|
1 |
|
'region', |
132
|
1 |
|
'The AWS "region" is required to run Amazon SQS context.' |
133
|
|
|
); |
134
|
|
|
|
135
|
1 |
|
return (new SqsConnectionFactory($contextConfig))->createContext(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|