Core/Handler/CommandHandler/InvokeGlobalFunctionHandler.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 60 CC 5 SCAR 26.1
CC
5
Cov
0%
process
30.0
new
2.0
BEGIN
2.0
CRAP 13.7
1 package Javonet::Core::Handler::CommandHandler::InvokeGlobalFunctionHandler;
2 use strict;
3 use warnings FATAL => 'all';
4 use lib 'lib';
5 use Moose;
6 use Nice::Try;
7 use aliased 'Javonet::Core::Exception::Exception' => 'Exception';
8 extends 'Javonet::Core::Handler::CommandHandler::AbstractCommandHandler';
9
10 sub new {
11 my $class = shift;
12 my $self = {
13 required_parameters_count => 1
14 };
15 return bless $self, $class;
16 }
17
18 sub process {
19 my ($self, $command) = @_;
20 try {
21 # Get the payload array reference and convert to array
22 my $payload_ref = $command->{payload};
23 my @payload = @$payload_ref;
24 my $parameters_length = scalar @payload;
25
26 # Verify at least one parameter is provided
27 if ($parameters_length < $self->{required_parameters_count}) {
28 die Exception->new("InvokeGlobalFunctionHandler parameters mismatch: requires at least one parameter (fully qualified method name)");
29 }
30
31 # The first parameter should be the fully qualified method name
32 my $fully_qualified_method = $payload[0];
33
34 # Extract the package name and method name using a regex
35 unless ($fully_qualified_method =~ /^(.*)::([^:]+)$/) {
36 die Exception->new("Invalid fully qualified method name: $fully_qualified_method");
37 }
38 my ($package, $function_name) = $fully_qualified_method =~ /^(.*)::([^:]+)$/;
39
40 # Attempt to load the package (if not already loaded)
41 eval "require $package";
42 if ($@) {
43 die Exception->new("Failed to load package '$package': $@");
44 }
45 #
46 # Check if the package can perform the method
47 my $code_ref = $package->can($function_name);
48 unless ($code_ref) {
49 die Exception->new("Method '$function_name' not found in package '$package'");
50 }
51
52 # Any additional payload elements are passed as arguments to the method
53 my @args = @payload[1 .. $#payload];
54
55 # Invoke the method with the arguments and return the result
56 return $code_ref->(@args);
57 }
58 catch ($e) {
59 return Exception->new($e);
60 }
61 }
62
63 1;