blib/lib/Message/Router.pm

filter: Statementstmt 100% 113 / 113 Branchbran 80.0% 64 / 80 Conditioncond 57.8% 22 / 38 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 82.5% 217 / 263 CC 60 SCAR 35.5
CC
60
Cov
86%
mroute
49.2
mroute_config
24.1
BEGIN
1.0
CRAP 34.9
↑ Summary
1 package Message::Router;
2 5 $Message::Router::VERSION = '1.161240';
3 5 ▶use strict;use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Message::Match qw(mmatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Message::Transform qw(mtransform);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 require Exporter;
8 5 ▶use vars qw(@ISA @EXPORT_OK $config);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 @ISA = qw(Exporter);
10 5 @EXPORT_OK = qw(mroute mroute_config);
11  
12 18 ▶sub mroute_config {
Subroutine mroute_config: called CC=24 SCAR=31.8
Pod documented
13 18 my $new_config;
14 18 eval {
15 18 ▶ $new_config = shift
Branch: 1 branch
BranchTrueFalseTotal
$new_config = shift() or die "single argument must be a HASH reference" 17 1 18
16 or die 'single argument must be a HASH reference';
17 17 ▶ die 'single argument must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
if shift() 1 16 17
18 if shift;
19 16 ▶ die 'single argument must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
unless $new_config and ref $new_config eq "HASH" 1 15 16
Condition66%2/3
$new_config and ref $new_config eq "HASH"
!ll&&!rl&&r
0115
MC/DC50%1/2
$new_config and ref $new_config eq "HASH"
$new_config ref $new_config eq "HASH"
Truth table66%2/3
A and B
A= $new_configB= ref $new_config eq "HASH"
ABresult
0X0
100
111
20 if not $new_config or not ref $new_config eq 'HASH';
21 die "passed config must have an ARRAY or HASH 'routes' key"
22 15 ▶ if not $new_config->{routes};
Branch: 1 branch
BranchTrueFalseTotal
unless $new_config->{"routes"} 1 14 15
23 14 ▶ if( ref $new_config->{routes} ne 'ARRAY' and
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_config->{"routes"} ne "ARRAY" and ref $new_config->{"routes"} ne "HASH") 1 13 14
Condition100%3/3
ref $new_config->{"routes"} ne "ARRAY" and ref $new_config->{"routes"} ne "HASH"
!ll&&!rl&&r
1121
MC/DC100%2/2
ref $new_config->{"routes"} ne "ARRAY" and ref $new_config->{"routes"} ne "HASH"
ref $new_config->{"routes"} ne "ARRAY" ref $new_config->{"routes"} ne "HASH"
Truth table100%3/3
A and B
A= ref $new_config->{"routes"} ne "ARRAY"B= ref $new_config->{"routes"} ne "HASH"
ABresult
0X0
100
111
24 ref $new_config->{routes} ne 'HASH') {
25 1 die "passed config must have an ARRAY or HASH 'routes' key"
26 }
27 13 ▶ if(ref $new_config->{routes} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_config->{"routes"} eq "ARRAY") 11 2 13
28 11 foreach my $route (@{$new_config->{routes}}) {
29 12 ▶ die "each route must be a HASH reference"
Branch: 1 branch
BranchTrueFalseTotal
unless $route 1 11 12
30 if not $route;
31 11 ▶ die "each route must be a HASH reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $route eq "HASH" 1 10 11
32 if not ref $route eq 'HASH';
33 die "each route has to have a HASH reference 'match' key"
34 10 ▶ if not $route->{match};
Branch: 1 branch
BranchTrueFalseTotal
unless $route->{"match"} 1 9 10
35 die "each route has to have a HASH reference 'match' key"
36 9 ▶ if not ref $route->{match} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $route->{"match"} eq "HASH" 0 9 9
37 9 ▶ if($route->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"transform"}) 4 5 9
38 die "the optional 'transform' key must be a HASH reference"
39 4 ▶ if ref $route->{transform} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $route->{"transform"} ne "HASH" 1 3 4
40 }
41 8 ▶ if($route->{forwards}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"forwards"}) 8 0 8
42 die "the optional 'forwards' key must be an ARRAY reference"
43 8 ▶ if ref $route->{forwards} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $route->{"forwards"} ne "ARRAY" 1 7 8
44 7 foreach my $forward (@{$route->{forwards}}) {
45 7 ▶ die 'each forward must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
unless $forward 0 7 7
46 if not $forward;
47 7 ▶ die 'each forward must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $forward ne "HASH" 1 6 7
48 if ref $forward ne 'HASH';
49 die "each forward must have a scalar 'handler' key"
50 6 ▶ if not $forward->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $forward->{"handler"} 1 5 6
51 die "each forward must have a scalar 'handler' key"
52 5 ▶ if ref $forward->{handler};
Branch: 1 branch
BranchTrueFalseTotal
if ref $forward->{"handler"} 1 4 5
53 }
54 }
55 }
56 }
57 };
58 18 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 13 5 18
59 13 die "Message::Router::mroute_config: $@\n";
60 }
61 5 $config = $new_config;
62 5 return $config;
63 }
64  
65 7 ▶sub mroute {
Subroutine mroute: called CC=37 SCAR=39.0
Pod documented
66 7 eval {
67 7 ▶ my $message = shift or die 'single argument must be a HASH reference';
Branch: 1 branch
BranchTrueFalseTotal
my $message = shift() or die "single argument must be a HASH reference" 6 1 7
68 6 ▶ die 'single argument must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $message and ref $message eq "HASH" 1 5 6
Condition66%2/3
ref $message and ref $message eq "HASH"
!ll&&!rl&&r
105
MC/DC50%1/2
ref $message and ref $message eq "HASH"
ref $message ref $message eq "HASH"
Truth table66%2/3
A and B
A= ref $messageB= ref $message eq "HASH"
ABresult
0X0
100
111
69 unless ref $message and ref $message eq 'HASH';
70 5 ▶ die 'single argument must be a HASH reference'
Branch: 1 branch
BranchTrueFalseTotal
if shift() 0 5 5
71 if shift;
72 5 ▶ if( $message->{static_forwards} and
Branch: 1 branch
BranchTrueFalseTotal
if ($message->{"static_forwards"} and ref $message->{"static_forwards"} and ref $message->{"static_forwards"} eq "ARRAY" and scalar @{$message->{"static_forwards"};}) 1 4 5
Condition75%6/8
$message->{"static_forwards"} and ref $message->{"static_forwards"} and ref $message->{"static_forwards"} eq "ARRAY" and scalar @{$message->{"static_forwards"};}
!ll
41
$message->{"static_forwards"} and ref $message->{"static_forwards"} and ref $message->{"static_forwards"} eq "ARRAY"
!ll&&!rl&&r
401
$message->{"static_forwards"} and ref $message->{"static_forwards"}
!ll&&!rl&&r
401
MC/DC33%1/3
$message->{"static_forwards"} and ref $message->{"static_forwards"} and ref $message->{"static_forwards"} eq "ARRAY" and scalar @{$message->{"static_forwards"};}
$message->{"static_forwards"} ref $message->{"static_forwards"} ref $message->{"static_forwards"} eq "ARRAY"
Truth table50%2/4
A and B and C
A= $message->{"static_forwards"}B= ref $message->{"static_forwards"}C= ref $message->{"static_forwards"} eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
73 ref $message->{static_forwards} and
74 ref $message->{static_forwards} eq 'ARRAY' and
75 1 scalar @{$message->{static_forwards}}) {
76 1 my $forward_recs = shift @{$message->{static_forwards}};
77 1 ▶ delete $message->{static_forwards} unless scalar @{$message->{static_forwards}};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$message->{"static_forwards"};} 1 0 1
78 1 ▶ die 'static_forwards: defined forward must be an ARRAY reference'
Branch: 1 branch
BranchTrueFalseTotal
if not ref $forward_recs or ref $forward_recs ne "ARRAY" 0 1 1
Condition33%1/3
not ref $forward_recs or ref $forward_recs ne "ARRAY"
l!l&&r!l&&!r
001
MC/DC0%0/2
not ref $forward_recs or ref $forward_recs ne "ARRAY"
not ref $forward_recs ref $forward_recs ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $forward_recsB= ref $forward_recs ne "ARRAY"
ABresult
1X1
011
000
79 if not ref $forward_recs or ref $forward_recs ne 'ARRAY';
80 1 foreach my $forward_rec (@{$forward_recs}) {
81 die 'static_forwards: defined forward must contain a forward that is a HASH reference'
82 if not $forward_rec->{forward} or
83 not ref $forward_rec->{forward} or
84 1 ▶ ref $forward_rec->{forward} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if not $forward_rec->{"forward"} && ref $forward_rec->{"forward"} or ref $forward_rec->{"forward"} ne "HASH" 0 1 1
Condition33%2/6
not $forward_rec->{"forward"} && ref $forward_rec->{"forward"} or ref $forward_rec->{"forward"} ne "HASH"
l!l&&r!l&&!r
001
$forward_rec->{'forward'} && ref $forward_rec->{'forward'}
!ll&&!rl&&r
001
MC/DC0%0/3
not $forward_rec->{"forward"} && ref $forward_rec->{"forward"} or ref $forward_rec->{"forward"} ne "HASH"
$forward_rec->{'forward'} ref $forward_rec->{'forward'} ref $forward_rec->{"forward"} ne "HASH"
Truth table25%1/4
not(A && B) or C
A= $forward_rec->{'forward'}B= ref $forward_rec->{'forward'}C= ref $forward_rec->{"forward"} ne "HASH"
ABCresult
0XX1
10X1
1111
1100
85 1 my $message = Storable::dclone $message;
86 1 ▶ if($forward_rec->{log_history}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($forward_rec->{"log_history"}) 1 0 1
87 1 $forward_rec = Storable::dclone $forward_rec;
88 $message->{'.static_forwards_log'} = {
89 forward_history => []
90 1 ▶ } unless $message->{'.static_forwards_log'};
Branch: 1 branch
BranchTrueFalseTotal
unless $message->{".static_forwards_log"} 1 0 1
91 1 push @{$message->{'.static_forwards_log'}->{forward_history}}, $forward_rec;
92 }
93 1 ▶ if( $forward_rec->{transform} and
Branch: 1 branch
BranchTrueFalseTotal
if ($forward_rec->{"transform"} and ref $forward_rec->{"transform"} and ref $forward_rec->{"transform"} eq "HASH") 1 0 1
Condition33%2/6
$forward_rec->{"transform"} and ref $forward_rec->{"transform"} and ref $forward_rec->{"transform"} eq "HASH"
!ll&&!rl&&r
001
$forward_rec->{"transform"} and ref $forward_rec->{"transform"}
!ll&&!rl&&r
001
MC/DC0%0/3
$forward_rec->{"transform"} and ref $forward_rec->{"transform"} and ref $forward_rec->{"transform"} eq "HASH"
$forward_rec->{"transform"} ref $forward_rec->{"transform"} ref $forward_rec->{"transform"} eq "HASH"
Truth table25%1/4
A and B and C
A= $forward_rec->{"transform"}B= ref $forward_rec->{"transform"}C= ref $forward_rec->{"transform"} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
94 ref $forward_rec->{transform} and
95 ref $forward_rec->{transform} eq 'HASH') {
96 1 mtransform($message, $forward_rec->{transform});
97 }
98 1 eval {
99 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
100 $forward_rec->{forward}->{handler} = 'IPC::Transit::Router::handler'
101 1 ▶ unless $forward_rec->{forward}->{handler};
Branch: 1 branch
BranchTrueFalseTotal
unless $forward_rec->{"forward"}{"handler"} 0 1 1
102 1 &{$forward_rec->{forward}->{handler}}(
103 message => $message,
104 forward => $forward_rec->{forward}
105 1 );
106 };
107 1 ▶ die "static_forwards: handler failed: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
108 }
109 1 return 1;
110 }
111 4 my @routes;
112 4 ▶ if(ref $config->{routes} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $Message::Router::config->{'routes'} eq 'ARRAY') { } 3 1 4
elsif (ref $Message::Router::config->{'routes'} eq 'HASH') { } 1 0 1
113 3 @routes = @{$config->{routes}};
114 } elsif(ref $config->{routes} eq 'HASH') {
115 1 foreach my $order (sort { $a <=> $b } keys %{$config->{routes}}) {
116 2 push @routes, $config->{routes}->{$order};
117 }
118 }
119 ROUTE:
120 4 foreach my $route (@routes) {
121 5 my $did_short_circuit = eval {
122 5 ▶ if(mmatch($message, $route->{match})) {
Branch: 1 branch
BranchTrueFalseTotal
if (mmatch($message, $route->{"match"})) 4 0 4
123 4 ▶ if($route->{transform}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"transform"}) 4 0 4
124 4 mtransform($message, $route->{transform});
125 }
126 4 ▶ if($route->{forwards}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{"forwards"}) 4 0 4
127 4 foreach my $forward (@{$route->{forwards}}) {
128 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 4 &{$forward->{handler}}(
130 message => $message,
131 route => $route,
132 routes => $config->{routes},
133 4 forward => $forward
134 );
135 }
136 }
137 4 ▶ if( $route->{'.router_control'} and
Branch: 1 branch
BranchTrueFalseTotal
if ($route->{".router_control"} and ref $route->{".router_control"} eq "HASH" and $route->{".router_control"}{"short_circuit"}) 1 3 4
Condition66%4/6
$route->{".router_control"} and ref $route->{".router_control"} eq "HASH" and $route->{".router_control"}{"short_circuit"}
!ll&&!rl&&r
301
$route->{".router_control"} and ref $route->{".router_control"} eq "HASH"
!ll&&!rl&&r
301
MC/DC33%1/3
$route->{".router_control"} and ref $route->{".router_control"} eq "HASH" and $route->{".router_control"}{"short_circuit"}
$route->{".router_control"} ref $route->{".router_control"} eq "HASH" $route->{".router_control"}{"short_circuit"}
Truth table50%2/4
A and B and C
A= $route->{".router_control"}B= ref $route->{".router_control"} eq "HASH"C= $route->{".router_control"}{"short_circuit"}
ABCresult
0XX0
10X0
1100
1111
138 ref $route->{'.router_control'} eq 'HASH' and
139 $route->{'.router_control'}->{short_circuit}) {
140 1 return 1;
141 }
142 }
143 3 return 0;
144 };
145 5 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 4 5
146 1 die "Message::Router::mroute: $@\n";
147 }
148 4 ▶ last if $did_short_circuit;
Branch: 1 branch
BranchTrueFalseTotal
if $did_short_circuit 1 3 4
149 }
150 };
151 7 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 4 7
152 3 die "Message::Router::mmatch: $@\n";
153 }
154 4 return 1;
155 }
156 5 1;
157  
158 __END__
↑ Summary