File Coverage

blib/lib/CMS/Drupal.pm
Criterion Covered Total %
statement 41 42 97.6
branch 11 18 61.1
condition 1 3 33.3
subroutine 9 9 100.0
pod 1 1 100.0
total 63 73 86.3


line stmt bran cond sub pod time code
1             package CMS::Drupal;
2             $CMS::Drupal::VERSION = '0.94';
3             # ABSTRACT: Perl interface to the Drupal CMS
4              
5 3     3   71492 use strict;
  3         9  
  3         68  
6 3     3   14 use warnings;
  3         7  
  3         66  
7              
8 3     3   2337 use Moo;
  3         41872  
  3         18  
9 3     3   39409 use Types::Standard qw/ Optional Maybe Str Int slurpy Dict /;
  3         691784  
  3         38  
10 3     3   6204 use CMS::Drupal::Types qw/ DBName DBDriver DBUsername DBPassword DBHost DBPort DBPrefix /;
  3         9  
  3         35  
11 3     3   14877 use Type::Params qw/ compile /;
  3         80095  
  3         44  
12              
13 3     3   92554 use DBI;
  3         141747  
  3         261  
14 3     3   37 use Carp qw/ confess croak /;
  3         7  
  3         1474  
15              
16             sub dbh {
17 13     13 1 27392 my $self = shift;
18 13 50       58 return $self->{'_dbh'} if defined( $self->{'_dbh'} );
19              
20             # We want to accept no params if we have the values in the env. But
21             # we don't want to use those creds when we are running certain tests
22             my $args = (exists $ENV{'DRUPAL_TEST_CREDS'} and ! exists $ENV{'DRUPAL_IGNORE_TEST_CREDS'}) ?
23 13 50 33     96 { split(',', $ENV{'DRUPAL_TEST_CREDS'}) } :
24             { @_ };
25              
26 13 100       63 confess "Fatal error! No database name provided! " unless exists $args->{'database'};
27 12 100       57 confess "Fatal error! No dbi:driver provided! " unless exists $args->{'driver'};
28              
29 11         51 my %types = (
30             'database' => DBName,
31             'driver' => DBDriver,
32             'username' => DBUsername,
33             'password' => DBPassword,
34             'host' => DBHost,
35             'port' => DBPort,
36             'prefix' => DBPrefix,
37             );
38              
39 11         308 for ( keys %{ $args } ) {
  11         68  
40 20 50       1413 next unless exists $types{ $_ }; # throw away unknown params
41 20         90 my $validate = compile( slurpy Dict [ $_ => $types{ $_ } ]);
42 20         219038 my ( $param ) = $validate->( $_ => $args->{ $_ } );
43             }
44              
45 1         159 my $dsn = join( ':', 'dbi', $args->{'driver'}, $args->{'database'} );
46 1 50       5 exists $args->{'host'} and $dsn .= ";host=$args->{'host'}";
47 1 50       4 exists $args->{'port'} and $dsn .= ";port=$args->{'port'}";
48 1 50       5 my $username = exists $args->{'username'} ? $args->{'username'} : '';
49 1 50       5 my $password = exists $args->{'password'} ? $args->{'password'} : '';
50 1         12 $self->{'_dbh'} = DBI->connect( $dsn, $username, $password, { 'RaiseError' => 1 } );
51              
52 0           return $self->{'_dbh'};
53             }
54              
55             1; ## return true to end package CMS::Drupal
56              
57             __END__