Skip to content

Commit f5a678d

Browse files
committed
Functionality and checks related to adding producers data to ProducersDataBag move to ProducersDataBag
1 parent 9981520 commit f5a678d

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/AbstractDataBag.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class AbstractDataBag
1616
/**
1717
* @var array
1818
*/
19-
private $data = [];
19+
protected $data = [];
2020

2121

2222
public function __construct(array $data)

src/DI/Helpers/ProducersHelper.php

+1-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ final class ProducersHelper extends AbstractHelper
2020
{
2121

2222
public const DELIVERY_MODES = [
23-
Producer::DELIVERY_MODE_PERSISTENT,
23+
Producer::DELIVERY_MODE_NON_PERSISTENT,
2424
Producer::DELIVERY_MODE_PERSISTENT,
2525
];
2626

@@ -45,22 +45,6 @@ public function setup(ContainerBuilder $builder, array $config = []): ServiceDef
4545
$producerData
4646
);
4747

48-
if (!in_array($producerConfig['deliveryMode'], self::DELIVERY_MODES, true)) {
49-
throw new \InvalidArgumentException(
50-
"Unknown exchange type [{$producerConfig['type']}]"
51-
);
52-
}
53-
54-
/**
55-
* 1, Producer has to be subscribed to either a queue or an exchange
56-
* 2, A producer can be subscribed to both a queue and an exchange
57-
*/
58-
if (empty($producerConfig['queue']) && empty($producerConfig['exchange'])) {
59-
throw new \InvalidArgumentException(
60-
'Producer has to be subscribed to either a queue or an exchange'
61-
);
62-
}
63-
6448
$producersConfig[$producerName] = $producerConfig;
6549
}
6650

src/Producer/ProducersDataBag.php

+41
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,48 @@
1111
namespace Gamee\RabbitMQ\Producer;
1212

1313
use Gamee\RabbitMQ\AbstractDataBag;
14+
use Gamee\RabbitMQ\DI\Helpers\ProducersHelper;
1415

1516
final class ProducersDataBag extends AbstractDataBag
1617
{
18+
19+
/**
20+
* @throws \InvalidArgumentException
21+
*/
22+
public function __construct(array $data)
23+
{
24+
foreach ($data as $producerName => $producer) {
25+
$this->addProducerByData($producerName, $producer);
26+
}
27+
}
28+
29+
30+
/**
31+
* @throws \InvalidArgumentException
32+
*/
33+
public function addProducerByData(string $producerName, array $data): void
34+
{
35+
$data['deliveryMode'] = $data['deliveryMode'] ?? Producer::DELIVERY_MODE_PERSISTENT;
36+
$data['contentType'] = $data['contentType'] ?? 'text/plain';
37+
$data['exchange'] = $data['exchange'] ?? null;
38+
$data['queue'] = $data['queue'] ?? null;
39+
40+
if (!in_array($data['deliveryMode'], ProducersHelper::DELIVERY_MODES, true)) {
41+
throw new \InvalidArgumentException(
42+
"Unknown exchange type [{$data['type']}]"
43+
);
44+
}
45+
46+
/**
47+
* 1, Producer has to be subscribed to either a queue or an exchange
48+
* 2, A producer can be subscribed to both a queue and an exchange
49+
*/
50+
if (empty($data['queue']) && empty($data['exchange'])) {
51+
throw new \InvalidArgumentException(
52+
'Producer has to be subscribed to either a queue or an exchange'
53+
);
54+
}
55+
56+
$this->data[$producerName] = $data;
57+
}
1758
}

0 commit comments

Comments
 (0)