-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConfig.php
More file actions
143 lines (123 loc) · 3.12 KB
/
Config.php
File metadata and controls
143 lines (123 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Octava\Bundle\JobQueueBundle;
use JMS\JobQueueBundle\Entity\Job;
/**
* Class Config
* @package Octava\Bundle\JobQueueBundle
*/
class Config
{
const NODE_DEFAULT_QUEUE = 'default_queue';
const NODE_LOCK_COMMANDS = 'lock_commands';
const NODE_QUEUE_DELIMITER = 'queue_delimiter';
/**
* @var array
*/
protected $config;
/**
* Config constructor.
* @param array $config
* @param array $options
*/
public function __construct(array $config, array $options = [])
{
$this->config = $config;
$this->options = $options;
$this->validate();
}
/**
* @return string[]
*/
public function getQueues()
{
$result = [$this->config[self::NODE_DEFAULT_QUEUE]];
if (!empty($this->options)) {
$result = array_keys($this->options);
}
return $result;
}
/**
* Список очередей для конктреного сервера
* @return array
*/
public function getRestrictedQueues()
{
$result = [];
$result[] = $this->getDefaultQueue();
foreach ($this->getLockCommands() as $command) {
$result[] = $this->buildLockQueue($this->getDefaultQueue(), $command);
}
return $result;
}
/**
* @return mixed
*/
public function getDefaultQueue()
{
return $this->config[self::NODE_DEFAULT_QUEUE];
}
/**
* @return array
*/
public function getLockCommands()
{
return array_filter($this->config[self::NODE_LOCK_COMMANDS]);
}
/**
* @return array
*/
public function getLockQueues()
{
$commands = $this->getLockCommands();
$result = [];
foreach ($this->getQueues() as $queue) {
foreach ($commands as $command) {
$result[] = $this->buildLockQueue($queue, $command);
}
}
return $result;
}
/**
* @param string $queue
* @param string $command
* @return string
*/
public function buildQueueName($queue, $command)
{
$result = $queue;
if (in_array($command, $this->getLockCommands())) {
$result = $this->buildLockQueue($queue, $command);
}
return $result;
}
/**
* @param string $queue
* @param string $command
* @return string
*/
protected function buildLockQueue($queue, $command)
{
return $queue.$this->getDelimiter().$command;
}
/**
* @return string
*/
protected function getDelimiter()
{
return $this->config[self::NODE_QUEUE_DELIMITER];
}
/**
* @throws \InvalidArgumentException
*/
protected function validate()
{
if (!empty($this->options)
&& Job::DEFAULT_QUEUE !== $this->getDefaultQueue()
&& !array_key_exists($this->getDefaultQueue(), $this->options)
) {
throw new \InvalidArgumentException(
sprintf('"%s" queue name not found in jms_job_queue.queue_options', $this->getDefaultQueue())
);
}
}
}