feat: add Messenger class for multiple adapter failover support#115
feat: add Messenger class for multiple adapter failover support#115TorstenDittmann wants to merge 2 commits intomainfrom
Conversation
This commit introduces the Messenger class, which enables automatic failover
across multiple messaging adapters. If one adapter throws an exception, the
next adapter in the sequence is tried until one succeeds or all fail.
Features:
- Accepts a single Adapter or an array of Adapters
- Tries adapters sequentially on exception
- Validates adapter compatibility (same type and message type)
- Returns the first successful response
- Throws aggregated exception with details if all adapters fail
- Supports SMS, Email, Push, and any other adapter types
Example usage:
$messenger = new Messenger([
new Twilio('sid', 'token'),
new Vonage('key', 'secret'),
]);
$result = $messenger->send($message);
Changes:
- Add src/Utopia/Messaging/Messenger.php
- Add tests/Messaging/MessengerTest.php with comprehensive test coverage
- Update README.md with usage example
Closes: feature request for multiple adapter support
Greptile SummaryThis PR introduces the Key highlights:
Two minor observations:
Confidence Score: 5/5Safe to merge — all remaining findings are P2 style/consistency suggestions that do not affect correctness or reliability. All previously flagged concerns have been addressed in this revision. The two remaining comments are minor: an inconsistent exception type in send() and an unenforced docblock promise on getMaxMessagesPerRequest(). Neither causes incorrect runtime behavior. The core failover logic, validation, and tests are solid. src/Utopia/Messaging/Messenger.php — minor exception-type inconsistency and unenforced limit contract in send() Important Files Changed
Reviews (2): Last reviewed commit: "fix: harden Messenger validation and rev..." | Re-trigger Greptile |
Validate Messenger adapter arrays at runtime so invalid elements fail with a clear InvalidArgumentException instead of a PHP error. This keeps the new Adapter|Adapter[] constructor ergonomic without weakening input validation. Also replace Messenger sprintf-based error construction with direct string concatenation, pluralize the single-adapter failure message correctly, and add test coverage for single-adapter construction and invalid array elements.
Summary
This PR introduces the
Messengerclass, which enables automatic failover across multiple messaging adapters. This addresses the feature request to allow using multiple adapters where if one fails, it falls back to the next one.Changes
New Class:
Utopia\Messaging\MessengerAdapteror an array ofAdapter[]New Tests:
tests/Messaging/MessengerTest.phpDocumentation: Updated README.md with usage example
Usage Example
Behavior
maxMessagesPerRequestacross all adaptersTesting
All tests pass:
Checklist