forked from php-enqueue/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicMetaRegistry.php
More file actions
80 lines (70 loc) · 1.78 KB
/
TopicMetaRegistry.php
File metadata and controls
80 lines (70 loc) · 1.78 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
<?php
namespace Enqueue\Client\Meta;
class TopicMetaRegistry
{
/**
* @var array
*/
protected $meta;
/**
* $meta = [
* 'aTopicName' => [
* 'description' => 'A desc',
* 'processors' => ['aProcessorNameFoo', 'aProcessorNameBar],
* ],
* ].
*
* @param array $meta
*/
public function __construct(array $meta)
{
$this->meta = $meta;
}
/**
* @param string $topicName
* @param string $description
*/
public function add($topicName, $description = null)
{
$this->meta[$topicName] = [
'description' => $description,
'processors' => [],
];
}
/**
* @param string $topicName
* @param string $processorName
*/
public function addProcessor($topicName, $processorName)
{
if (false == array_key_exists($topicName, $this->meta)) {
$this->add($topicName);
}
$this->meta[$topicName]['processors'][] = $processorName;
}
/**
* @param string $topicName
*
* @return TopicMeta
*/
public function getTopicMeta($topicName)
{
if (false == array_key_exists($topicName, $this->meta)) {
throw new \InvalidArgumentException(sprintf('The topic meta not found. Requested name `%s`', $topicName));
}
$topic = array_replace([
'description' => '',
'processors' => [],
], $this->meta[$topicName]);
return new TopicMeta($topicName, $topic['description'], $topic['processors']);
}
/**
* @return \Generator|TopicMeta[]
*/
public function getTopicsMeta()
{
foreach (array_keys($this->meta) as $topicName) {
yield $this->getTopicMeta($topicName);
}
}
}