| 1 | package Javonet::Core::Handler::CommandHandler::InvokeInstanceMethodHandler; | ||
| 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 => 2 | ||
| 14 | }; | ||
| 15 | return bless $self, $class; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub process { | ||
| 19 | my ($self, $command) = @_; | ||
| 20 | try { | ||
| 21 | my $current_payload_ref = $command->{payload}; | ||
| 22 | my @cur_payload = @$current_payload_ref; | ||
| 23 | my $parameters_length = @cur_payload; | ||
| 24 | if ($parameters_length < $self->{required_parameters_count}) { | ||
| 25 | die Exception->new("Exception: InvokeInstanceMethod parameters mismatch"); | ||
| 26 | } | ||
| 27 | |||
| 28 | my $instance = $command->{payload}[0]; | ||
| 29 | my $method_name = $command->{payload}[1]; | ||
| 30 | if ($instance->can($method_name)) { | ||
| 31 | if ($parameters_length == $self->{required_parameters_count}) { | ||
| 32 | return $instance->$method_name(); | ||
| 33 | } | ||
| 34 | else { | ||
| 35 | my @method_arguments; | ||
| 36 | for (my $i = 2; $i < $parameters_length; $i++) { | ||
| 37 | push @method_arguments, $command->{payload}[$i]; | ||
| 38 | } | ||
| 39 | return $instance->$method_name(@method_arguments); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | die Exception->new("Exception InvokeInstanceMethod: method not found"); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | catch ($e){ | ||
| 47 | return Exception->new($e); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |