-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathConsumeMySqlDomainEventsCommand.php
More file actions
59 lines (47 loc) · 1.8 KB
/
ConsumeMySqlDomainEventsCommand.php
File metadata and controls
59 lines (47 loc) · 1.8 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
<?php
declare(strict_types=1);
namespace CodelyTv\Apps\Mooc\Backend\Command\DomainEvents\MySql;
use CodelyTv\Shared\Domain\Bus\Event\DomainEvent;
use CodelyTv\Shared\Infrastructure\Bus\Event\DomainEventSubscriberLocator;
use CodelyTv\Shared\Infrastructure\Bus\Event\MySql\MySqlDoctrineDomainEventsConsumer;
use CodelyTv\Shared\Infrastructure\Doctrine\DatabaseConnections;
use Override;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function Lambdish\Phunctional\pipe;
#[AsCommand(name: 'codely:domain-events:mysql:consume', description: 'Consume domain events from MySql',)]
final class ConsumeMySqlDomainEventsCommand extends Command
{
public function __construct(
private readonly MySqlDoctrineDomainEventsConsumer $consumer,
private readonly DatabaseConnections $connections,
private readonly DomainEventSubscriberLocator $subscriberLocator
) {
parent::__construct();
}
#[Override]
protected function configure(): void
{
$this->addArgument('quantity', InputArgument::REQUIRED, 'Quantity of events to process');
}
#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$quantityEventsToProcess = (int) $input->getArgument('quantity');
$consumer = pipe($this->consumer(), fn () => $this->connections->clear());
$this->consumer->consume($consumer, $quantityEventsToProcess);
return 0;
}
private function consumer(): callable
{
return function (DomainEvent $domainEvent): void {
$subscribers = $this->subscriberLocator->allSubscribedTo($domainEvent::class);
foreach ($subscribers as $subscriber) {
$subscriber($domainEvent);
}
};
}
}