S
S
Steve2021-07-12 10:17:54
symfony
Steve, 2021-07-12 10:17:54

Call commandBus once?

How can I make the commandBus be called after the loop once, and not on every iteration?

foreach ($dCampaigns as $dCampaign) {
            $search = $dCampaign->getSearch();
            $groupId = $this->userRepository->findById($dCampaign->getUserId())->getGroupId();
            $contactCollection = $this->contactRepository->findByTagIds(false, false, $search, $groupId);

            if (null !== $contactCollection) {
                for ($i = 0; $i < count($contactCollection); $i++) {
                    if (null === $contactCollection[$i]->getEmail()) continue;

                    $command->subject = $dCampaign->getSubject();
                    $command->body = $dCampaign->getContent();
                    $command->contact = $contactCollection[$i];
                    $command->userEmail = $dCampaign->getSenderEmail();
                    $command->campaignId = $dCampaign->getIdValue();
                    $command->replyToEmail = $dCampaign->getReplyToEmail();

                    $this->commandBus->handle($command);
                }
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-07-12
@Maksclub

Make a command that will contain a bunch of other commands
And make a lisener that can process it
And then once: in the mini-command cycle, collect it into a bunch-command, after handle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question