Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MessengerBundle] only allow to list ListableReceiverInterface #2127

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ public function listReceiverMessageCountAction(ReceiversRepositoryInterface $rec
return $this->json(['data' => $receivers, 'success' => true]);
}

public function listListableReceiversAction(ReceiversRepositoryInterface $receiverLocator): Response
{
$receivers = [];
foreach ($receiverLocator->getListableReceiversMapping() as $name => $receiver) {
$receivers[] = [
'receiver' => $name,
'count' => $receiver instanceof MessageCountAwareInterface ? $receiver->getMessageCount() : null,
];
}

return $this->json(['data' => $receivers, 'success' => true]);
}

public function listFailureReceiversAction(FailureReceiversRepositoryInterface $failureReceivers): Response
{
$receivers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

use Symfony\Component\Messenger\Exception\RuntimeException;

final class FailureReceiverNotListableException extends RuntimeException
final class ReceiverNotListableException extends RuntimeException
{
public function __construct(
) {
parent::__construct('The failure receiver does not support listing or showing specific messages.');
public function __construct()
{
parent::__construct('The receiver does not support listing or showing specific messages.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace CoreShop\Bundle\MessengerBundle\Messenger;

use CoreShop\Bundle\MessengerBundle\Exception\FailureReceiverNotListableException;
use CoreShop\Bundle\MessengerBundle\Exception\ReceiverNotListableException;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;

final class FailedMessageRejecter implements FailedMessageRejecterInterface
Expand All @@ -33,7 +33,7 @@ public function rejectStoredMessage(string $receiverName, int $id): void
$failureReceiver = $this->failureReceivers->getFailureReceiver($receiverName);

if (!$failureReceiver instanceof ListableReceiverInterface) {
throw new FailureReceiverNotListableException();
throw new ReceiverNotListableException();
}

$envelope = $failureReceiver->find($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace CoreShop\Bundle\MessengerBundle\Messenger;

use CoreShop\Bundle\MessengerBundle\Exception\FailureReceiverNotListableException;
use CoreShop\Bundle\MessengerBundle\Exception\ReceiverNotListableException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
Expand All @@ -37,7 +37,7 @@ public function listFailedMessages(string $receiverName, int $limit = 10): array
$receiver = $this->failureReceivers->getFailureReceiver($receiverName);

if (!$receiver instanceof ListableReceiverInterface) {
throw new FailureReceiverNotListableException();
throw new ReceiverNotListableException();
}

$envelopes = $receiver->all($limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace CoreShop\Bundle\MessengerBundle\Messenger;

use CoreShop\Bundle\MessengerBundle\Exception\FailureReceiverNotListableException;
use CoreShop\Bundle\MessengerBundle\Exception\ReceiverNotListableException;
use CoreShop\Bundle\MessengerBundle\Stamp\RetriedByUserStamp;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -38,7 +38,7 @@ public function retryFailedMessage(string $receiver, int $id): void
$failureReceiver = $this->failureReceivers->getFailureReceiver($receiver);

if (!$failureReceiver instanceof ListableReceiverInterface) {
throw new FailureReceiverNotListableException();
throw new ReceiverNotListableException();
}

$envelope = $failureReceiver->find($id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace CoreShop\Bundle\MessengerBundle\Messenger;

use CoreShop\Bundle\MessengerBundle\Exception\FailureReceiverNotListableException;
use CoreShop\Bundle\MessengerBundle\Exception\ReceiverNotListableException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
Expand All @@ -35,7 +35,7 @@ public function listMessages(string $receiverName, int $limit = 10): array
$receiver = $this->receivers->getReceiver($receiverName);

if (!$receiver instanceof ListableReceiverInterface) {
throw new FailureReceiverNotListableException();
throw new ReceiverNotListableException();
}

$envelopes = $receiver->all($limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace CoreShop\Bundle\MessengerBundle\Messenger;

use CoreShop\Bundle\MessengerBundle\Exception\ReceiverDoesNotExistException;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;

Expand All @@ -43,6 +44,26 @@ public function getReceiversMapping(): array
return $receivers;
}


/**
* @return ReceiverInterface[]
*/
public function getListableReceiversMapping(): array
{
$receivers = [];
foreach ($this->receiverNames as $receiverName) {
$receiver = $this->getReceiver($receiverName);

if (!$receiver instanceof ListableReceiverInterface) {
continue;
}

$receivers[$receiverName] = $receiver;
}

return $receivers;
}

public function getReceiver(string $receiverName): ReceiverInterface
{
if (!\in_array($receiverName, $this->receiverNames, true) || !$this->receiverLocator->has($receiverName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ interface ReceiversRepositoryInterface
*/
public function getReceiversMapping(): array;

/**
* @return ReceiverInterface[]
*/
public function getListableReceiversMapping(): array;

public function getReceiver(string $receiverName): ReceiverInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ coreshop_admin_messenger_count:
options:
expose: true

coreshop_admin_messenger_list_receivers:
path: /admin/messenger/list-receivers
defaults: { _controller: CoreShop\Bundle\MessengerBundle\Controller\ListMessagesController::listListableReceiversAction }
options:
expose: true

coreshop_admin_messenger_list_failed:
path: /admin/messenger/list-failed/{receiverName}
defaults: { _controller: CoreShop\Bundle\MessengerBundle\Controller\ListMessagesController::listFailedMessagesAction }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ coreshop.messenger.list = Class.create({
store: {
proxy: {
type: 'ajax',
url: Routing.generate('coreshop_admin_messenger_count'),
url: Routing.generate('coreshop_admin_messenger_list_receivers'),
reader: {
type: 'json',
rootProperty: 'data'
Expand Down