alexandrebulete / ddd-comment-bundle
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
Requires
- php: ^8.4
- alexandrebulete/ddd-antispam-bundle: ^1.0
- alexandrebulete/ddd-doctrine-bridge: ^1.5
- alexandrebulete/ddd-foundation: ^1.6
- alexandrebulete/ddd-sylius-bundle: ^1.6
- alexandrebulete/ddd-symfony-bundle: ^1.6
- doctrine/dbal: ^4.3
- doctrine/doctrine-bundle: ^2.13 || ^3.0
- doctrine/doctrine-migrations-bundle: ^3.7 || ^4.0
- doctrine/migrations: ^3.8
- doctrine/orm: ^3.3
- pagerfanta/core: ^4.0
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- psr/log: ^3.0
- sylius/admin-ui: ^0.14
- sylius/grid-bundle: ^1.13
- sylius/resource-bundle: ^1.14
- symfony/clock: ^7.0 || ^8.0
- symfony/config: ^7.0 || ^8.0
- symfony/dependency-injection: ^7.0 || ^8.0
- symfony/event-dispatcher: ^7.0 || ^8.0
- symfony/form: ^7.0 || ^8.0
- symfony/framework-bundle: ^7.0 || ^8.0
- symfony/http-foundation: ^7.0 || ^8.0
- symfony/rate-limiter: ^7.3 || ^8.0
- symfony/routing: ^7.0 || ^8.0
- symfony/security-core: ^7.3 || ^8.0
- symfony/security-csrf: ^7.0 || ^8.0
- symfony/service-contracts: ^3.0
- symfony/translation: ^7.0 || ^8.0
- symfony/translation-contracts: ^3.0
- symfony/uid: ^7.0 || ^8.0
- symfony/validator: ^7.0 || ^8.0
- twig/twig: ^3.10
- webmozart/assert: ^1.11
Requires (Dev)
- alexandrebulete/ddd-notification-bundle: ^1.2
- deptrac/deptrac: ^4.7
- phpstan/phpstan: ^2.1
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^12.0 || ^13.0
Suggests
- alexandrebulete/ddd-notification-bundle: A notice on the channel of your choice (email, Slack, SMS…) for each comment posted
Provides
None
Conflicts
None
Replaces
None
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, withddd-sylius-bundle's sections) — open or closed, how many, how many to moderate, and a link to the queue filtered on that content. Shown tocomment.read, switchable bycomment.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