https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=43663 Bug ID: 43663 Summary: Add Koha::Objects::Mixin::Polymorphic for polymorphic subclass dispatch Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: enhancement Priority: P5 - low Component: Architecture, internals, and plumbing Assignee: koha-bugs@lists.koha-community.org Reporter: martin.renvoize@openfifth.co.uk QA Contact: testopia@bugs.koha-community.org Target Milestone: --- Several Koha::Objects plural classes independently implement the same pattern: given a row, decide which Koha::Object subclass should represent it, based on the value of one column (a hand-rolled _polymorphic_field/_polymorphic_map pair plus an ~8-line object_class method). Koha::File::Transports (already in main) dispatches on the 'transport' column to Koha::File::Transport::{SFTP,FTP,Local}. Koha::Auth::Identity::Providers (bug 24880, not yet merged) dispatches on 'protocol' to Koha::Auth::Identity::Provider::{OAuth,OIDC,SAML2}. The same need came up again during review of bug 42870 (pluggable email transports). Koha::Auth::Identity::Provider::OAuth additionally hand-types its own dispatch key a second time, independently, inside its own new() ($params->{protocol} = 'OAuth';) - nothing ties that string to the _polymorphic_map entry in the plural class that has to match it, so the two can silently drift apart. This bug extracts the shared behaviour into a proper Koha::Objects::Mixin::* (the same convention Koha already uses for Koha::Objects::Mixin::AdditionalFields, composed via 'use parent qw( Koha::Objects Koha::Objects::Mixin::Foo )', used by 20+ plural classes), and migrates the one already-in-main consumer (Koha::File::Transports) onto it as proof it is behaviour-preserving. A consuming plural class declares: sub _polymorphic_field { return 'transport' } sub _polymorphic_classes { return (List::Of::Class::Names) } sub _polymorphic_base_class { return 'Fallback::Class::Name' } Each subclass self-declares its own dispatch key: sub _polymorphic_key { return 'sftp' } The mixin builds _polymorphic_map from _polymorphic_classes plus each class's own _polymorphic_key, and provides object_class() itself, so the copy-pasted dispatch logic disappears from every consumer, not just the map. A plural class needing genuinely custom mapping logic can still override _polymorphic_map (or object_class) directly - ordinary Perl inheritance makes that a free escape hatch. This is a proposal filed ahead of code, based on a design worked out with the QA team. It is deliberately narrow: it introduces no mechanism for Koha Plugins to register additional polymorphic subclasses, no Plugin management UI changes, and no koha-conf.xml changes - that is a separate, later concern once a concrete capability needs it. It does not touch Koha::Auth::Identity::Providers (bug 24880) or bug 42870 directly; both could adopt this mixin as a follow-up once merged. Patches to follow. Test plan (once patches are attached): 1. Apply the patch(es). 2. Run prove t/db_dependent/Koha/Objects/Mixin/Polymorphic.t and confirm all tests pass. 3. Run prove t/db_dependent/Koha/File/Transport.t t/db_dependent/Koha/File/Transports.t t/db_dependent/Koha/File/Transport/FTP.t t/db_dependent/Koha/File/Transport/Local.t t/db_dependent/Koha/File/Transport/SFTP.t and confirm they all still pass unmodified against the migrated Koha::File::Transports, proving the migration is behaviour-preserving. -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.