Lim/RPC/Transport/Clients.pm

filter: Statementstmt 27.5% 22 / 80 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.3% 7 / 12 Podpod 100% 3 / 3 totaltotal 21.9% 32 / 146 CC 24 SCAR 44.5
CC
24
Cov
18%
load
182.0
transport
72.0
_new
12.0
CRAP 86.0
« Lim/RPC/Transport/Client/RabbitMQ.pm ↑ Summary Lim/RPC/Transport/RabbitMQ.pm »
1 package Lim::RPC::Transport::Clients;
2  
3 10 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Module::Find qw(findsubmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =encoding utf8
13  
14 =head1 NAME
15  
16 Lim::RPC::Transport::Clients - Lim's RPC transport client loader and container
17  
18 =head1 VERSION
19  
20 See L<Lim> for version.
21  
22 =cut
23  
24 10 our $VERSION = $Lim::VERSION;
25 10 our $INSTANCE;
26  
27 =head1 SYNOPSIS
28  
29 use Lim::RPC::Transport::Clients;
30 $transport = Lim::RPC::Transport::Clients->instance->transport('name');
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =cut
37  
38 0 ▶sub _new {
Subroutine _new: not called CC=3 SCAR=24.8
39 0 my $this = shift;
40 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
41 0 my %args = ( @_ );
42 0 my $self = {
43 logger => Log::Log4perl->get_logger($class),
44 transport => {},
45 transport_name => {}
46 };
47 0 bless $self, $class;
48 0 weaken($self->{logger});
49  
50 0 $self->load;
51  
52 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("new ", __PACKAGE__, " ", $self) 0 0 0
53 0 $self;
54 }
55  
56 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
57 0 my ($self) = @_;
58 0 ▶ Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self);
Branch: 1 branch
BranchTrueFalseTotal
Lim::OBJ_DEBUG() and $self->{"logger"}->debug("destroy ", __PACKAGE__, " ", $self) 0 0 0
59  
60 0 delete $self->{transport};
61 }
62  
63 10 ▶END {
Subroutine END: called CC=1 SCAR=0.0
64 10 undef($INSTANCE);
65 }
66  
67 =item $instance = Lim::RPC::Transport::Clients->instance
68  
69 Returns the singelton instance of this class.
70  
71 =cut
72  
73 0 ▶sub instance {
Subroutine instance: not called CC=2 SCAR=17.9
Pod documented
74 0 ▶ $INSTANCE ||= Lim::RPC::Transport::Clients->_new;
Condition0%0/3
$Lim::RPC::Transport::Clients::INSTANCE ||= "Lim::RPC::Transport::Clients"->_new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Lim::RPC::Transport::Clients::INSTANCE ||= "Lim::RPC::Transport::Clients"->_new
$Lim::RPC::Transport::Clients::INSTANCE "Lim::RPC::Transport::Clients"->_new
Truth table0%0/3
A ||= B
A= $Lim::RPC::Transport::Clients::INSTANCEB= "Lim::RPC::Transport::Clients"->_new
ABresult
1X1
011
000
75 }
76  
77 =item $instance->load
78  
79 Loads all classes that exists on the system under Lim::RPC::Transport::Client::.
80 Returns the reference to itself even on error.
81  
82 =cut
83  
84 0 ▶sub load {
Subroutine load: not called CC=13 SCAR=52.0
Pod documented
85 0 my ($self) = @_;
86  
87 0 foreach my $module (findsubmod Lim::RPC::Transport::Client) {
88 0 ▶ if (exists $self->{transport}->{$module}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transport"}{$module}) 0 0 0
89 0 ▶ Lim::WARN and $self->{logger}->warn('Transport client ', $module, ' already loaded');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Transport client ", $module, " already loaded") 0 0 0
90 0 next;
91 }
92  
93 0 ▶ if ($module =~ /^([\w:]+)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /^([\w:]+)$/ou) { } 0 0 0
94 0 $module = $1;
95 }
96 else {
97 0 next;
98 }
99  
100 0 my $name;
101 0 eval {
102 0 eval "require $module;";
103 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
104 0 $name = $module->name;
105 };
106  
107 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
108 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to load transport client ', $module, ': ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to load transport client ", $module, ": ", $@) 0 0 0
109 0 $self->{transport}->{$module} = {
110 name => $name,
111 module => $module,
112 loaded => 0,
113 error => $@
114 };
115 0 next;
116 }
117  
118 0 ▶ unless ($name =~ /^[a-z0-9_\-\.]+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^[a-z0-9_\-\.]+$/ou) 0 0 0
119 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to load transport client ', $module, ': Illegal characters in transport name');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to load transport client ", $module, ": Illegal characters in transport name") 0 0 0
120 0 $self->{transport}->{$module} = {
121 module => $module,
122 loaded => 0,
123 error => 'Illegal characters in transport name'
124 };
125 0 next;
126 }
127  
128 0 ▶ if (exists $self->{transport_name}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"transport_name"}{$name}) 0 0 0
129 0 ▶ Lim::WARN and $self->{logger}->warn('Transport name ', $name, ' already loaded by module ', $self->{transport_name}->{$name});
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Transport name ", $name, " already loaded by module ", $self->{"transport_name"}{$name}) 0 0 0
130 0 next;
131 }
132  
133 0 ▶ Lim::DEBUG and $self->{logger}->debug('Loaded ', $module);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Loaded ", $module) 0 0 0
134 0 $self->{transport}->{$module} = {
135 name => $name,
136 module => $module,
137 version => $module->VERSION,
138 loaded => 1
139 };
140 0 $self->{transport_name}->{$name} = $module;
141 }
142  
143 0 $self;
144 }
145  
146 =item $transport = $instance->transport($name, ...)
147  
148 =cut
149  
150 0 ▶sub transport {
Subroutine transport: not called CC=8 SCAR=42.8
Pod documented
151 0 my $self = shift;
152 0 my $name = shift;
153  
154 0 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
155 0 my $module;
156  
157 0 foreach (keys %{$self->{transport}}) {
158 0 ▶ if ($self->{transport}->{$_}->{loaded} and $self->{transport}->{$_}->{name} eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name) 0 0 0
Condition0%0/3
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"transport"}{$_}{"loaded"} and $self->{"transport"}{$_}{"name"} eq $name
$self->{"transport"}{$_}{"loaded"} $self->{"transport"}{$_}{"name"} eq $name
Truth table0%0/3
A and B
A= $self->{"transport"}{$_}{"loaded"}B= $self->{"transport"}{$_}{"name"} eq $name
ABresult
0X0
100
111
159 0 $module = $self->{transport}->{$_}->{module};
160 0 last;
161 }
162 }
163  
164 0 ▶ if (defined $module) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $module) 0 0 0
165 0 my $transport;
166 0 eval {
167 0 $transport = $module->new(@_);
168 };
169 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
170 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to create new instance of transport client ', $name, '(', $module, '): ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to create new instance of transport client ", $name, "(", $module, "): ", $@) 0 0 0
171 }
172 else {
173 0 return $transport;
174 }
175 }
176 }
177 0 return;
178 }
179  
180 =back
181  
182 =head1 AUTHOR
183  
184 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
185  
186 =head1 BUGS
187  
188 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
189  
190 =head1 SUPPORT
191  
192 You can find documentation for this module with the perldoc command.
193  
194 perldoc Lim::RPC::Transport::Clients
195  
196 You can also look for information at:
197  
198 =over 4
199  
200 =item * Lim issue tracker (report bugs here)
201  
202 L<https://github.com/jelu/lim/issues>
203  
204 =back
205  
206 =head1 ACKNOWLEDGEMENTS
207  
208 =head1 LICENSE AND COPYRIGHT
209  
210 Copyright 2012-2013 Jerry Lundström.
211  
212 This program is free software; you can redistribute it and/or modify it
213 under the terms of either: the GNU General Public License as published
214 by the Free Software Foundation; or the Artistic License.
215  
216 See http://dev.perl.org/licenses/ for more information.
217  
218  
219 =cut
220  
221 10 1; # End of Lim::RPC::Transport::Clients
« Lim/RPC/Transport/Client/RabbitMQ.pm ↑ Summary Lim/RPC/Transport/RabbitMQ.pm »