Search by

alexandrebulete / ddd-comment-bundle

Alexandre Buleté

Comments under any content as a reusable DDD Bounded Context: manual or automatic moderation in a Sylius back office, spam checks (ddd-antispam-bundle), per-kind and per-content opening, notices through ddd-notification-bundle.

Package info

github.com/AlexandreBulete/ddd-comment-bundle

Type:symfony-bundle

pkg:composer/alexandrebulete/ddd-comment-bundle

Statistics

Installs: 48

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

1.2.0 2026-11-27 15:11 UTC

This package is auto-updated.

Last update: 2026-11-27 15:11:50 UTC


README

Comments under any content, as a reusable DDD Bounded Context for Symfony: the application says what may be commented on, visitors write a name and a text, and a moderator decides in a Sylius back office — every comment by hand, or at once unless it looks like spam. Spam checks come from ddd-antispam-bundle, notices from ddd-notification-bundle when installed.

Installation

composer require alexandrebulete/ddd-comment-bundle
bin/console doctrine:migrations:migrate

The Flex recipe registers the bundle and imports config/routes.php (the bulk moderation action). The comment, comment_thread and comment_settings tables come with the bundle's own migration.

Model

Aggregate Holds
Comment its target, author name, body, status (pending, approved, spam), why it was held
CommentThread one content's comments as a whole: closed or open. Exists once a content was closed
CommentSettings moderation mode (manual, automatic), the kinds open to comments, links allowed, banned words

A comment targets a CommentTarget(kind, reference): review and a book id, post and an article id. The bundle never reads the content itself.

No email, no website field: nothing is kept that is not shown.

Declaring what may be commented on

One service per kind of content, implementing CommentTargetSourceInterface (autoconfigured):

final readonly class ReviewCommentSource implements CommentTargetSourceInterface
{
    public function kind(): string { return 'review'; }

    public function label(): string { return 'Reviews'; }   // or a translation key

    /** Titles and URLs for the back office, one query per kind. */
    public function describe(array $references): array { /* reference => DescribedTarget */ }
}

A kind with no source takes no comment. The settings then open or close each declared kind, and a moderator closes one content at a time.

Use cases

Use case Access
PostComment public: the form is the gate
FindCommentThread public: the approved comments of a target, and whether it is open
ApproveComment, HoldComment, MarkCommentAsSpam, DeleteComment, CloseCommentThread, ReopenCommentThread comment.moderate
FindComments, FindComment, CountPendingComments comment.read
FindCommentSettings, ChangeCommentSettings comment.configure

On arrival the status is decided once: suspicious → spam (kept, hidden, with its reasons); otherwise pending (manual) or approved (automatic).

The public form

#[Route('/reviews/{slug}', methods: ['GET', 'POST'])]
public function show(Request $request, CommentForm $comments, QueryBusInterface $queries): Response
{
    $target = new CommentTarget('review', $bookId);
    $thread = $queries->ask(new FindCommentThreadQuery($target)); // comments, open
    $form = $thread->open ? $comments->create() : null;

    if ($form !== null && $comments->handle($form, $request, $target)->isPosted()) {
        return $this->redirect(…); // post/redirect/get
    }

    return $this->render('review.html.twig', ['thread' => $thread, 'form' => $form]);
}

The layout is the application's. CommentOutcome tells Published from Held; a robot is answered Held too, and nothing is kept.

Spam

Through ddd-antispam-bundle, with its own purpose (comment) and rate limiter (comment_form): a trap field, a signed timing token, a limit per address, reCAPTCHA v3 when the site enables it. A robot's comment is dropped. Then the text is read against the settings — too many links, a banned word — and a doubtful one is held as spam, with its reasons, for a moderator to rescue.

Back office

  • Comments: the queue, newest first, filtered by status; approve, hold, mark as spam or delete one by one or in bulk. The menu shows how many wait.
  • Comment settings: moderation mode, open kinds, links allowed, banned words.
  • On a comment's screen: close its content's comments.
  • On a content's own screen, for the forms listed in comment.forms: a Comments card (side column, with ddd-sylius-bundle's sections) — open or closed, how many, how many to moderate, and a link to the queue filtered on that content. Shown to comment.read, switchable by comment.moderate.

Being told

With ddd-notification-bundle installed, the bundle declares the topic comment.posted: a notice for each comment that is not spam, saying whether it waits, with a link to the back office. Never the name nor the text.

Configuration

comment:
    spam:
        min_seconds: 3         # faster than this after display: a robot
        max_age: 86400         # an older form is a replay
        rate_limit: { limit: 5, interval: '15 minutes' }
    forms:                     # back-office forms that get a comments card
        App\Library\Infrastructure\Form\BookType: review  # form type => kind
    admin:
        enabled: true          # false for a headless deployment
        grid_limits: [20, 50, 100]

Seams

Ports bound in config/services.php, to rebind in the application:

Port Default Rebind for
DomainEventPublisherInterface ImmediateEventPublisher (in-process, in the transaction) an outbox
CommentRepositoryInterface, CommentThreadRepositoryInterface, CommentSettingsRepositoryInterface Doctrine another storage
CommentKindCatalogInterface the declared CommentTargetSourceInterface services another catalogue

Development

composer qa    # phpstan (max, strict) + deptrac + phpunit

Integration tests run on SQLite in memory, or on the database given by DDD_TEST_DATABASE_URL (CI: PostgreSQL, MySQL, SQLite).

License

MIT