OpenInteract2/Observer.pm

filter: Statementstmt 19.6% 24 / 122 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.8% 7 / 13 Podpod 0.0% 0 / 4 totaltotal 13.8% 31 / 224 CC 41 SCAR 45.5
CC
41
Cov
12%
register_observer
182.0
initialize
110.0
_register_initial_observers
90.0
CRAP 94.5
« OpenInteract2/Auth.pm ↑ Summary OpenInteract2/Manage/Website/TestLDAP.pm »
1 package OpenInteract2::Observer;
2  
3 # $Id: Observer.pm,v 1.4 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Config::IniFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 87 $OpenInteract2::Observer::VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
13  
14 87 my ( $log );
15  
16 0 ▶sub register_observer {
Subroutine register_observer: not called CC=14 SCAR=52.0
Pod undocumented
17 0 my ( $class, $observer_name, $observer_info, $registry ) = @_;
18 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
19  
20 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Trying to register observer '${observer_name}'") 0 0 0
21 $log->info( "Trying to register observer '$observer_name'" );
22 0 my ( $observer, $observer_type );
23  
24 my $observer_class = ( ref $observer_info )
25 ? $observer_info->{class}
26 0 ▶ : $observer_info;
Branch: 1 branch
BranchTrueFalseTotal
ref $observer_info ? : 0 0 0
27  
28 0 ▶ if ( $observer_class ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($observer_class) { } 0 0 0
elsif (my $observer_obj = $observer_info->{'object'}) { } 0 0 0
elsif (my $observer_sub = $observer_info->{'sub'}) { } 0 0 0
29 0 my $error = $class->_require_module( $observer_class );
30 0 ▶ unless ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
31 0 $observer = $observer_class;
32 0 $observer_type = 'class';
33 }
34 }
35 elsif ( my $observer_obj = $observer_info->{object} ) {
36 0 my $error = $class->_require_module( $observer_obj );
37 0 ▶ unless ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
38 0 $observer = eval { $observer_obj->new };
39 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
40 0 $log->error( "Failed to instantiate observer ",
41 "object '$observer_obj'" );
42 }
43 else {
44 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Created object '${observer_obj}' ok") 0 0 0
45 $log->info( "Created object '$observer_obj' ok" );
46 0 $observer_type = 'object';
47 }
48 }
49 }
50 elsif ( my $observer_sub = $observer_info->{sub} ) {
51 0 $observer_sub =~ /^(.*)::(.*)$/;
52 0 my ( $name_class, $name_sub ) = ( $1, $2 );
53 0 my $error = $class->_require_module( $name_class );
54 0 ▶ unless ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error) 0 0 0
55 87 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 0 $observer = *{ $name_class . '::' . $name_sub };
57 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Seemed to assign anonymous sub ok") 0 0 0
58 $log->info( "Seemed to assign anonymous sub ok" );
59 0 $observer_type = 'sub';
60 }
61 }
62 else {
63 0 $log->error( "No observer registered for '$observer_name': must specify",
64 " 'class', 'object' or 'sub' in observer information. ",
65 "(See docs for OpenInteract2::Action under ",
66 "'OBSERVABLE ACTIONS')" );
67 }
68 0 ▶ if ( $observer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($observer) 0 0 0
69 0 $registry->{ $observer_name } = $observer;
70 }
71 0 return $observer;
72 }
73  
74  
75 # Note: $action_item can be a name or an object
76  
77 0 ▶sub add_observer_to_action {
Subroutine add_observer_to_action: not called CC=7 SCAR=40.3
Pod undocumented
78 0 my ( $class, $observer_name, $action_item ) = @_;
79  
80 0 my $observer = CTX->lookup_observer( $observer_name );
81 0 ▶ return unless ( $observer );
Branch: 1 branch
BranchTrueFalseTotal
unless $observer 0 0 0
82 0 ▶ if ( blessed( $observer ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($observer)) 0 0 0
83 0 $observer = $observer->new; # create a new object
84 }
85  
86 0 my $observed = $action_item;
87 0 ▶ unless ( ref $observed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $observed) 0 0 0
88 0 my $action_info = eval { CTX->lookup_action_info( $action_item ) };
89 0 ▶ return if ( $@ or ref $action_info ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $action_info ne "HASH" 0 0 0
Condition0%0/3
$@ or ref $action_info ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $action_info ne "HASH"
$@ ref $action_info ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $action_info ne "HASH"
ABresult
1X1
011
000
90 0 $observed = $action_info->{class};
91 }
92 0 $observed->add_observer( $observer );
93 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Added observer '${observer}' to '${observed}' ok") 0 0 0
94 $log->info( "Added observer '$observer' to '$observed' ok" );
95 0 return $observer;
96 }
97  
98  
99 0 ▶sub initialize {
Subroutine initialize: not called CC=10 SCAR=47.0
Pod undocumented
100 0 my ( $class ) = @_;
101 0 my $li = get_logger( LOG_INIT );
102  
103 0 my $observer_file = $class->create_observer_filename;
104 0 ▶ return unless ( -f $observer_file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $observer_file 0 0 0
105 0 my $observer_ini = OpenInteract2::Config::IniFile->read_config({
106 filename => $observer_file
107 });
108 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
109 0 $li->error( "Failed to read '$observer_file': $@" );
110 0 return;
111 }
112 0 ▶ $li->is_info && $li->info( "Read in '$observer_file' ok" );
Branch: 1 branch
BranchTrueFalseTotal
$li->is_info and $li->info("Read in '${observer_file}' ok") 0 0 0
113  
114 my $observer_registry = $class->_register_initial_observers(
115 0 $observer_ini->{observer}, CTX->packages );
116 0 ▶ $li->is_info && $li->info( "Registered internal observers ok" );
Branch: 1 branch
BranchTrueFalseTotal
$li->is_info and $li->info("Registered internal observers ok") 0 0 0
117  
118 0 CTX->set_observer_registry( $observer_registry );
119  
120 0 my $mappings = $observer_ini->{map};
121 0 ▶ if ( ref $mappings eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mappings eq "HASH") 0 0 0
122 0 ▶ while ( my ( $observer_name, $action_info ) = each %{ $mappings } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$mappings;}) 0 0 0
123 my @actions = ( ref $action_info )
124 0 ▶ ? @{ $action_info } : ( $action_info );
Branch: 1 branch
BranchTrueFalseTotal
ref $action_info ? : 0 0 0
125 0 foreach my $action_name ( @actions ) {
126 0 ▶ $li->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$li->is_info and $li->info("Trying to add observer '${observer_name}' to ", "action '${action_name}'") 0 0 0
127 $li->info( "Trying to add observer '$observer_name' to ",
128 "action '$action_name'" );
129 0 $class->add_observer_to_action( $observer_name, $action_name );
130 }
131 }
132 }
133 0 return;
134 }
135  
136 0 ▶sub create_observer_filename {
Subroutine create_observer_filename: not called CC=1 SCAR=6.9
Pod undocumented
137 0 my ( $class ) = @_;
138 0 my $conf_dir = CTX->lookup_directory( 'config' );
139 0 return File::Spec->catfile( $conf_dir, 'observer.ini' );
140 }
141  
142 0 ▶sub _register_initial_observers {
Subroutine _register_initial_observers: not called CC=9 SCAR=45.0
143 0 my ( $class, $ini_observers, $packages ) = @_;
144 0 my $li = get_logger( LOG_INIT );
145  
146 0 my %observer_map = ();
147  
148 # First register observers in packages; entries in 'observer.ini' will
149 # override packages since it's assumed people editing it know what
150 # they're doing...
151  
152 0 foreach my $pkg ( @{ $packages } ) {
153 0 my $pkg_observers = $pkg->config->observer;
154 0 ▶ next unless ( ref $pkg_observers eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pkg_observers eq "HASH" 0 0 0
155 0 ▶ while ( my ( $observer_name, $observer_class ) = each %{ $pkg_observers } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$pkg_observers;}) 0 0 0
156 0 ▶ $li->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$li->is_info and $li->info("Registering observer '${observer_name}' as ", "'${observer_class}' from package ", $pkg->full_name) 0 0 0
157 $li->info( "Registering observer '$observer_name' as ",
158 "'$observer_class' from package ", $pkg->full_name );
159 0 $observer_map{ $observer_name } = $observer_class;
160 }
161 }
162  
163 # Now cycle through the INI and pull the info for each observer
164 # (usually just a class name)
165  
166 0 ▶ while ( my ( $observer_name, $observer_info ) = each %{ $ini_observers } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ini_observers;}) 0 0 0
167 0 ▶ $li->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$li->is_info and $li->info("Registering observer '${observer_name}' from ", "server config") 0 0 0
168 $li->info( "Registering observer '$observer_name' from ",
169 "server config" );
170 0 ▶ if ( $observer_map{ $observer_name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($observer_map{$observer_name}) 0 0 0
171 0 $li->warn( "WARNING: Overwriting observer '$observer_name', ",
172 "previously '$observer_map{ $observer_name }'" );
173 }
174 0 $observer_map{ $observer_name } = $observer_info;
175 }
176  
177 0 my %observer_registry = ();
178  
179 # Now that they're collected, be sure we can
180 # require/reference/instantiate each
181  
182 0 ▶ while ( my ( $observer_name, $observer_info ) = each %observer_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %observer_map) 0 0 0
183 0 $class->register_observer( $observer_name,
184 $observer_info,
185 \%observer_registry );
186 }
187 0 return \%observer_registry;
188 }
189  
190 0 ▶sub _require_module {
Subroutine _require_module: not called CC=5 SCAR=34.0
191 0 my ( $class, $to_require ) = @_;
192 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
193  
194 0 eval "require $to_require";
195 0 my $error = $@;
196 0 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) { } 0 0 0
197 0 $log->error( "Failed to require '$to_require': $error" );
198 }
199 else {
200 0 ▶ $log->is_info &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Required module '${to_require}' ok'") 0 0 0
201 $log->info( "Required module '$to_require' ok'" );
202 }
203 0 ▶ return ( $error ) ? $error : undef;
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
204 }
205  
206 87 1;
207  
208 __END__
« OpenInteract2/Auth.pm ↑ Summary OpenInteract2/Manage/Website/TestLDAP.pm »