Usul/TraitFor/ConnectInfo.pm

filter: Statementstmt 84.5% 82 / 97 Branchbran 54.5% 12 / 22 Conditioncond 36.8% 14 / 38 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 78.2% 18 / 23 Podpod 100% 4 / 4 totaltotal 63.1% 130 / 206 CC 27 SCAR 18.7
CC
27
Cov
69%
__ANON__
13.0
__ANON__
12.0
__ANON__
9.9
CRAP 6.5
« Usul/L10N.pm ↑ Summary Usul/Log.pm »
1 package Class::Usul::TraitFor::ConnectInfo;
2  
3 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Class::Usul::Constants qw( EXCEPTION_CLASS CONFIG_EXTN FALSE TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Class::Usul::Crypt::Util qw( decrypt_from_config );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Class::Usul::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Class::Usul::Functions qw( merge_attributes throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Unexpected::Functions qw( Unspecified );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 requires qw( config ); # As a class method
15  
16 2 my $_cache = {};
17  
18 # Private functions
19 4 ▶my $_connect_attr = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 4 return [ qw( class ctlfile ctrldir database dataclass_attr extension
21 prefix read_secure salt seed seed_file subspace tempdir ) ];
22 2 };
23  
24 6 ▶my $_get_cache_key = sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
25 6 my $param = shift;
26 my $db = $param->{database}
27 6 ▶ or throw 'Class [_1] has no database name', [ $param->{class} ];
Branch: 1 branch
BranchTrueFalseTotal
my $db = $param->{"database"} or &throw("Class [_1] has no database name", [$param->{"class"}]) 6 0 6
28  
29 6 ▶ return $param->{subspace} ? "${db}.".$param->{subspace} : $db;
Branch: 1 branch
BranchTrueFalseTotal
$param->{'subspace'} ? : 0 6 6
30 2 };
31  
32 2 ▶my $_get_credentials_file = sub {
Subroutine __ANON__: called CC=9 SCAR=25.6
33 2 my $param = shift; my $file = $param->{ctlfile};
34  
35 2 ▶ defined $file and -f $file and return $file;
Branch: 1 branch
BranchTrueFalseTotal
defined $file and -f $file and return $file 0 2 2
Condition66%2/3
defined $file and -f $file
!ll&&!rl&&r
110
MC/DC0%0/2
defined $file and -f $file
defined $file -f $file
Truth table66%2/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
36  
37 2 my $dir = $param->{ctrldir}; my $db = $param->{database};
38  
39 2 ▶ $dir or throw Unspecified, [ 'ctrldir' ];
Branch: 1 branch
BranchTrueFalseTotal
$dir or &throw(Unspecified(), ["ctrldir"]) 2 0 2
40 2 ▶ -d $dir or throw 'Directory [_1] not found', [ $dir ];
Branch: 1 branch
BranchTrueFalseTotal
-d $dir or &throw("Directory [_1] not found", [$dir]) 2 0 2
41 2 ▶ $db or throw 'Class [_1] has no database name', [ $param->{class} ];
Branch: 1 branch
BranchTrueFalseTotal
$db or &throw("Class [_1] has no database name", [$param->{"class"}]) 2 0 2
42  
43 2 ▶ $file = catfile( $dir, $db.($param->{extension} // CONFIG_EXTN) );
Condition66%2/3
$param->{"extension"} // &CONFIG_EXTN()
l!l&&r!l&&!r
110
MC/DC0%0/2
$param->{"extension"} // &CONFIG_EXTN()
$param->{"extension"} &CONFIG_EXTN()
Truth table66%2/3
A // B
A= $param->{"extension"}B= &CONFIG_EXTN()
ABresult
1X1
011
000
44  
45 2 ▶ -f $file and return $file;
Branch: 1 branch
BranchTrueFalseTotal
-f $file and return $file 2 0 2
46  
47 0 ▶ return catfile( $dir, 'connect-info'.($param->{extension} // CONFIG_EXTN) );
Condition0%0/3
$param->{"extension"} // &CONFIG_EXTN()
l!l&&r!l&&!r
000
MC/DC0%0/2
$param->{"extension"} // &CONFIG_EXTN()
$param->{"extension"} &CONFIG_EXTN()
Truth table0%0/3
A // B
A= $param->{"extension"}B= &CONFIG_EXTN()
ABresult
1X1
011
000
48 2 };
49  
50 2 ▶my $_get_dataclass_schema = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 return Class::Usul::File->dataclass_schema( @_ );
52 2 };
53  
54 2 ▶my $_unicode_options = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
55 2 return { mysql => { mysql_enable_utf8 => TRUE },
56 pg => { pg_enable_utf8 => TRUE },
57 sqlite => { sqlite_unicode => TRUE }, };
58 2 };
59  
60 0 ▶my $_dump_config_data = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my ($param, $cfg_data) = @_;
62  
63 0 my $ctlfile = $_get_credentials_file->( $param );
64 0 my $schema = $_get_dataclass_schema->( $param->{dataclass_attr} );
65  
66 0 return $schema->dump( { data => $cfg_data, path => $ctlfile } );
67 2 };
68  
69 2 ▶my $_extract_creds_from = sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
70 2 my ($param, $cfg_data) = @_; my $key = $_get_cache_key->( $param );
71  
72 2 ▶ ($cfg_data->{credentials} and defined $cfg_data->{credentials}->{ $key })
Branch: 1 branch
BranchTrueFalseTotal
$cfg_data->{"credentials"} and defined $cfg_data->{"credentials"}{$key} or &throw("Path [_1] database [_2] no credentials", [&$_get_credentials_file($param), $key]) 2 0 2
Condition33%1/3
$cfg_data->{"credentials"} and defined $cfg_data->{"credentials"}{$key}
!ll&&!rl&&r
002
MC/DC0%0/2
$cfg_data->{"credentials"} and defined $cfg_data->{"credentials"}{$key}
$cfg_data->{"credentials"} defined $cfg_data->{"credentials"}{$key}
Truth table33%1/3
A and B
A= $cfg_data->{"credentials"}B= defined $cfg_data->{"credentials"}{$key}
ABresult
0X0
100
111
73 or throw 'Path [_1] database [_2] no credentials',
74 [ $_get_credentials_file->( $param ), $key ];
75  
76 2 return $cfg_data->{credentials}->{ $key };
77 2 };
78  
79 2 ▶my $_get_connect_options = sub {
Subroutine __ANON__: called CC=7 SCAR=22.9
80 2 my $creds = shift;
81 my $uopt = $creds->{unicode_option}
82 2 ▶ // $_unicode_options->()->{ lc $creds->{driver} } // {};
Condition40%2/5
($creds->{"unicode_option"} // &$_unicode_options()->{lc $creds->{"driver"}}) // {}
l!l
20
$creds->{"unicode_option"} // &$_unicode_options()->{lc $creds->{"driver"}}
l!l&&r!l&&!r
020
MC/DC0%0/2
($creds->{"unicode_option"} // &$_unicode_options()->{lc $creds->{"driver"}}) // {}
$creds->{"unicode_option"} &$_unicode_options()->{lc $creds->{"driver"}}
Truth table33%1/3
A // B
A= $creds->{"unicode_option"}B= &$_unicode_options()->{lc $creds->{"driver"}}
ABresult
1X1
011
000
83  
84 return { AutoCommit => $creds->{auto_commit } // TRUE,
85 PrintError => $creds->{print_error } // FALSE,
86 RaiseError => $creds->{raise_error } // TRUE,
87 2 ▶ %{ $uopt }, %{ $creds->{database_attr} // {} }, };
Condition50%4/8
$creds->{"auto_commit"} // 1
l!l
02
$creds->{"print_error"} // 0
l!l
02
$creds->{"raise_error"} // 1
l!l
02
$creds->{"database_attr"} // {}
l!l
02
MC/DC0%0/1
$creds->{"auto_commit"} // 1
$creds->{"auto_commit"}
MC/DC0%0/1
$creds->{"print_error"} // 0
$creds->{"print_error"}
MC/DC0%0/1
$creds->{"raise_error"} // 1
$creds->{"raise_error"}
MC/DC0%0/1
$creds->{"database_attr"} // {}
$creds->{"database_attr"}
Truth table50%1/2
A
A= $creds->{"auto_commit"}
Aresult
11
00
Truth table50%1/2
A
A= $creds->{"print_error"}
Aresult
11
00
Truth table50%1/2
A
A= $creds->{"raise_error"}
Aresult
11
00
Truth table50%1/2
A
A= $creds->{"database_attr"}
Aresult
11
00
88 2 };
89  
90 2 ▶my $_load_config_data = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 2 my $schema = $_get_dataclass_schema->( $_[ 0 ]->{dataclass_attr} );
92  
93 2 return $schema->load( $_get_credentials_file->( $_[ 0 ] ) );
94 2 };
95  
96 # Private methods
97 0 ▶my $_merge_attributes = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
98 0 ▶ return merge_attributes { class => blessed $_[ 0 ] || $_[ 0 ] },
Condition0%0/5
builtin::blessed($_[0]) || $_[0]
l!l&&r!l&&!r
000
$_[2] // {}
l!l
00
MC/DC0%0/2
builtin::blessed($_[0]) || $_[0]
builtin::blessed($_[0]) $_[0]
MC/DC0%0/1
$_[2] // {}
$_[2]
Truth table0%0/3
A || B
A= builtin::blessed($_[0])B= $_[0]
ABresult
1X1
011
000
Truth table0%0/2
A
A= $_[2]
Aresult
11
00
99 $_[ 1 ], ($_[ 2 ] // {}), $_connect_attr->();
100 2 };
101  
102 # Public methods
103 0 ▶sub dump_config_data {
Subroutine dump_config_data: not called CC=1 SCAR=6.9
Pod documented
104 0 my ($self, $config, $db, $cfg_data) = @_;
105  
106 0 my $param = $self->$_merge_attributes( $config, { database => $db } );
107  
108 0 return $_dump_config_data->( $param, $cfg_data );
109 }
110  
111 0 ▶sub extract_creds_from {
Subroutine extract_creds_from: not called CC=1 SCAR=6.9
Pod documented
112 0 my ($self, $config, $db, $cfg_data) = @_;
113  
114 0 my $param = $self->$_merge_attributes( $config, { database => $db } );
115  
116 0 return $_extract_creds_from->( $param, $cfg_data );
117 }
118  
119 4 ▶sub get_connect_info {
Subroutine get_connect_info: called CC=7 SCAR=20.4
Pod documented
120 4 ▶ my ($self, $app, $param) = @_; $app //= $self; $param //= {};
Condition40%2/5
$app //= $self
l!l&&r!l&&!r
400
$param //= {}
l!l
40
MC/DC0%0/2
$app //= $self
$app $self
MC/DC0%0/1
$param //= {}
$param
Truth table33%1/3
A //= B
A= $appB= $self
ABresult
1X1
011
000
Truth table50%1/2
A
A= $param
Aresult
11
00
121  
122 4 merge_attributes $param, $app->config, $self->config, $_connect_attr->();
123  
124 4 ▶ my $class = $param->{class} = blessed $self || $self;
Condition33%1/3
builtin::blessed($self) || $self
l!l&&r!l&&!r
400
MC/DC0%0/2
builtin::blessed($self) || $self
builtin::blessed($self) $self
Truth table33%1/3
A || B
A= builtin::blessed($self)B= $self
ABresult
1X1
011
000
125 4 my $key = $_get_cache_key->( $param );
126  
127 4 ▶ defined $_cache->{ $key } and return $_cache->{ $key };
Branch: 1 branch
BranchTrueFalseTotal
defined $_cache->{$key} and return $_cache->{$key} 2 2 4
128  
129 2 my $cfg_data = $_load_config_data->( $param );
130 2 my $creds = $_extract_creds_from->( $param, $cfg_data );
131 2 my $dsn = 'dbi:'.$creds->{driver}.':database='.$param->{database};
132 2 my $password = decrypt_from_config $param, $creds->{password};
133 2 my $opts = $_get_connect_options->( $creds );
134  
135 2 ▶ $creds->{host} and $dsn .= ';host='.$creds->{host};
Branch: 1 branch
BranchTrueFalseTotal
$creds->{"host"} and $dsn .= ';host=' . $creds->{"host"} 2 0 2
136 2 ▶ $creds->{port} and $dsn .= ';port='.$creds->{port};
Branch: 1 branch
BranchTrueFalseTotal
$creds->{"port"} and $dsn .= ';port=' . $creds->{"port"} 2 0 2
137  
138 2 return $_cache->{ $key } = [ $dsn, $creds->{user}, $password, $opts ];
139 }
140  
141 0 ▶sub load_config_data {
Subroutine load_config_data: not called CC=1 SCAR=6.9
Pod documented
142 0 my ($self, $config, $db) = @_;
143  
144 0 my $param = $self->$_merge_attributes( $config, { database => $db } );
145  
146 0 return $_load_config_data->( $param );
147 }
148  
149 2 1;
150  
151 =pod
152  
153 =encoding utf-8
154  
155 =head1 Name
156  
157 Class::Usul::TraitFor::ConnectInfo - Provides the DBIC connect info array ref
158  
159 =head1 Synopsis
160  
161 package YourClass;
162  
163 use Moo;
164 use Class::Usul::Constants;
165 use Class::Usul::Types qw( NonEmptySimpleStr Object );
166  
167 with 'Class::Usul::TraitFor::ConnectInfo';
168  
169 has 'database' => is => 'ro', isa => NonEmptySimpleStr,
170 default => 'database_name';
171  
172 has 'schema' => is => 'lazy', isa => Object, builder => sub {
173 my $self = shift; my $extra = $self->config->connect_params;
174 $self->schema_class->connect( @{ $self->get_connect_info }, $extra ) };
175  
176 has 'schema_class' => is => 'ro', isa => NonEmptySimpleStr,
177 default => 'dbic_schema_class_name';
178  
179 sub config { # A class method
180 return { ...config parameters... }
181 }
182  
183 =head1 Description
184  
185 Provides the DBIC connect information array reference
186  
187 =head1 Configuration and Environment
188  
189 The JSON data looks like this:
190  
191 {
192 "credentials" : {
193 "schedule" : {
194 "driver" : "mysql",
195 "host" : "localhost",
196 "password" : "{Twofish}U2FsdGVkX1/xcBKZB1giOdQkIt8EFgfNDFGm/C+fZTs=",
197 "port" : "3306",
198 "user" : "username"
199 }
200 }
201 }
202  
203 where in this example C<schedule> is the database name
204  
205 =head1 Subroutines/Methods
206  
207 =head2 dump_config_data
208  
209 $dumped_data = $self->dump_config_data( $app_config, $db, $cfg_data );
210  
211 Call the L<dump method|File::DataClass::Schema/dump> to write the
212 configuration file back to disk
213  
214 =head2 extract_creds_from
215  
216 $creds = $self->extract_creds_from( $app_config, $db, $cfg_data );
217  
218 Returns the credential info for the specified database and (optional)
219 subspace. The subspace attribute of C<$app_config> is appended
220 to the database name to create a unique cache key
221  
222 =head2 get_connect_info
223  
224 $db_info_arr = $self->get_connect_info( $app_config, $db );
225  
226 Returns an array ref containing the information needed to make a
227 connection to a database; DSN, user id, password, and options hash
228 ref. The data is read from the configuration file in the config
229 C<ctrldir>. Multiple sets of data can be stored in the same file,
230 keyed by the C<$db> argument. The password is decrypted if
231 required
232  
233 =head2 load_config_data
234  
235 $cfg_data = $self->load_config_data( $app_config, $db );
236  
237 Returns a hash ref of configuration file data. The path to the file
238 can be specified in C<< $app_config->{ctlfile} >> or it will default
239 to the F<connect-info.$extension> file in the C<< $app_config->{ctrldir} >>
240 directory. The C<$extension> is either C<< $app_config->{extension} >>
241 or C<< $self->config->{extension} >> or the default extension given
242 by the C<CONFIG_EXTN> constant
243  
244 =head1 Diagnostics
245  
246 None
247  
248 =head1 Dependencies
249  
250 =over 3
251  
252 =item L<Moo::Role>
253  
254 =item L<Class::Usul::Crypt::Util>
255  
256 =item L<Class::Usul::File>
257  
258 =item L<Unexpected>
259  
260 =back
261  
262 =head1 Incompatibilities
263  
264 There are no known incompatibilities in this module
265  
266 =head1 Bugs and Limitations
267  
268 There are no known bugs in this module.
269 Please report problems to the address below.
270 Patches are welcome
271  
272 =head1 Acknowledgements
273  
274 Larry Wall - For the Perl programming language
275  
276 =head1 Author
277  
278 Peter Flanigan, C<< <pjfl@cpan.org> >>
279  
280 =head1 License and Copyright
281  
282 Copyright (c) 2018 Peter Flanigan. All rights reserved
283  
284 This program is free software; you can redistribute it and/or modify it
285 under the same terms as Perl itself. See L<perlartistic>
286  
287 This program is distributed in the hope that it will be useful,
288 but WITHOUT WARRANTY; without even the implied warranty of
289 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
290  
291 =cut
292  
293 # Local Variables:
294 # mode: perl
295 # tab-width: 3
296 # End:
« Usul/L10N.pm ↑ Summary Usul/Log.pm »