Bug 16586: [QA Follow-up] Simplify code
[koha_ffzg] / Koha / Plugins.pm
index a188d7c..c0598b1 100644 (file)
@@ -21,6 +21,7 @@ use Modern::Perl;
 
 use Module::Load::Conditional qw(can_load);
 use Module::Pluggable search_path => ['Koha::Plugin'], except => qr/::Edifact(|::Line|::Message|::Order|::Segment|::Transport)$/;
+use List::MoreUtils qw( any );
 
 use C4::Context;
 use C4::Output;
@@ -46,18 +47,26 @@ sub new {
     return bless( $args, $class );
 }
 
-=head2 GetPlugins()
+=head2 GetPlugins
 
-This will return a list of all the available plugins of the passed type.
+This will return a list of all available plugins, optionally limited by
+method or metadata value.
 
-Usage: my @plugins = C4::Plugins::GetPlugins( $method );
+    my @plugins = C4::Plugins::GetPlugins({
+        method => 'some_method',
+        metadata => { some_key => 'some_value' },
+    });
+
+The method and metadata parameters are optional.
+Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
+If you pass multiple keys in the metadata hash, all keys must match.
 
-At the moment, the available types are 'report', 'tool' and 'to_marc'.
 =cut
 
 sub GetPlugins {
-    my $self   = shift;
-    my $method = shift;
+    my ( $self, $params ) = @_;
+    my $method = $params->{method};
+    my $req_metadata = $params->{metadata} // {};
 
     my @plugin_classes = $self->plugins();
     my @plugins;
@@ -68,13 +77,13 @@ sub GetPlugins {
 
             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
 
-            if ($method) {
-                if ( $plugin->can($method) ) {
-                    push( @plugins, $plugin );
-                }
-            } else {
-                push( @plugins, $plugin );
-            }
+            # Limit results by method or metadata
+            next if $method && !$plugin->can($method);
+            my $plugin_metadata = $plugin->get_metadata;
+            next if $plugin_metadata
+                and %$req_metadata
+                and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata;
+            push @plugins, $plugin;
         }
     }
     return @plugins;