Skip to content

[13.x] Add enum support to ChannelManager#59716

Closed
sumaiazaman wants to merge 2 commits into
laravel:13.xfrom
sumaiazaman:feat/channel-manager-enum-support
Closed

[13.x] Add enum support to ChannelManager#59716
sumaiazaman wants to merge 2 commits into
laravel:13.xfrom
sumaiazaman:feat/channel-manager-enum-support

Conversation

@sumaiazaman

Copy link
Copy Markdown
Contributor

Summary

This PR adds enum support to ChannelManager::channel() and ChannelManager::deliverVia() using the enum_value() helper, aligning it with other manager classes that already support this pattern.

Context

Several manager classes already accept enums for connection/driver names:

Class Supports Enums
Manager::driver() ✅ (merged in #59659)
CacheManager::store() ✅ (merged in #59637)
AuthManager::guard() ✅ (merged in #59646)
MailManager::mailer() ✅ (merged in #59645)
QueueManager::connection() ✅ (merged in #59389)
LogManager::channel() ✅ (merged in #59391)
FilesystemManager::disk()
RedisManager::connection()
ChannelManager::channel()
ChannelManager::deliverVia()

This inconsistency means developers who define notification channel names as enums cannot pass them directly to Notification::channel() like they can with Queue::connection() or Cache::store(). More importantly, passing an enum to deliverVia() silently stores the enum object in $defaultChannel instead of its string value, causing a mismatch when the value is later used as a driver key.

Solution

  • Updated @param docblock on channel() to accept \UnitEnum|string|null — the parent Manager::driver() already calls enum_value(), so no logic change is needed here
  • Added enum_value() call in deliverVia() to ensure the string value is stored in $defaultChannel
  • Updated @param docblock on deliverVia() to accept \UnitEnum|string

Example

enum NotificationChannel: string
{
    case Mail     = 'mail';
    case Database = 'database';
}

// Before: deliverVia() silently stored the enum object, breaking driver resolution
Notification::deliverVia(NotificationChannel::Database);

// After: works as expected
Notification::channel(NotificationChannel::Mail)->send(...);
Notification::deliverVia(NotificationChannel::Database);

Why This Doesn't Break Existing Features

  • enum_value() returns the input unchanged for strings and null — all existing calls behave identically
  • The channel() change is docblock-only; the underlying Manager::driver() already handles enum resolution
  • Follows the exact same pattern already established across CacheManager, AuthManager, MailManager, QueueManager, LogManager, FilesystemManager, RedisManager, and the base Manager class

Changes

  • src/Illuminate/Notifications/ChannelManager.php — Added enum_value() in deliverVia(); updated docblocks on channel() and deliverVia()
  • tests/Notifications/NotificationChannelManagerTest.php — Added 2 test cases

Test Plan

  • testChannelManagerCanResolveBackedEnumChannel — Verifies backed enum resolves to the correct channel driver
  • testDeliverViaAcceptsBackedEnum — Verifies the string value is stored in $defaultChannel, not the enum object
  • Existing tests pass (no breaking changes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants