-
Notifications
You must be signed in to change notification settings - Fork 4
/
AmqpMessageProducerPluginFactory.php
62 lines (53 loc) · 1.93 KB
/
AmqpMessageProducerPluginFactory.php
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
<?php
/**
* This file is part of the prooph/humus-amqp-producer.
* (c) 2016-2019 Alexander Miertsch <kontakt@codeliner.ws>
* (c) 2016-2019 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Prooph\ServiceBus\Message\HumusAmqp\Container;
use Prooph\ServiceBus\Exception;
use Prooph\ServiceBus\Plugin\MessageProducerPlugin;
use Psr\Container\ContainerInterface;
final class AmqpMessageProducerPluginFactory
{
/**
* @var string
*/
private $amqpMessageProducerName;
/**
* Creates a new instance from a specified config, specifically meant to be used as static factory.
*
* In case you want to use another config key than provided by the factories, you can add the following factory to
* your config:
*
* <code>
* <?php
* return [
* 'your_amqp_message_producer' => [AmqpMessageProducerFactory::class, 'your_amqp_message_producer_name'],
* ];
* </code>
*
* @throws Exception\InvalidArgumentException
*/
public static function __callStatic(string $amqpMessageProducerName, array $arguments): MessageProducerPlugin
{
if (! isset($arguments[0]) || ! $arguments[0] instanceof ContainerInterface) {
throw new Exception\InvalidArgumentException(
\sprintf('The first argument must be of type %s', ContainerInterface::class)
);
}
return (new static($amqpMessageProducerName))->__invoke($arguments[0]);
}
public function __construct(string $amqpMessageProducerName)
{
$this->amqpMessageProducerName = $amqpMessageProducerName;
}
public function __invoke(ContainerInterface $container): MessageProducerPlugin
{
return new MessageProducerPlugin($container->get($this->amqpMessageProducerName));
}
}