File Coverage

blib/lib/CMS/Drupal/Modules/MembershipEntity.pm
Criterion Covered Total %
statement 65 72 90.2
branch 12 16 75.0
condition 2 5 40.0
subroutine 9 9 100.0
pod 1 1 100.0
total 89 103 86.4


line stmt bran cond sub pod time code
1             package CMS::Drupal::Modules::MembershipEntity;
2             $CMS::Drupal::Modules::MembershipEntity::VERSION = '0.96';
3             # ABSTRACT: Perl interface to Drupal MembershipEntity entities
4              
5 10     10   1758855 use strict;
  10         26  
  10         251  
6 10     10   52 use warnings;
  10         14  
  10         314  
7              
8 10     10   13608 use Moo;
  10         20355  
  10         72  
9 10     10   28676 use Types::Standard qw/ :all /;
  10         111035  
  10         112  
10 10     10   433344 use Time::Local;
  10         14513  
  10         682  
11 10     10   65 use Carp qw/ carp croak /;
  10         16  
  10         465  
12              
13 10     10   6514 use CMS::Drupal::Modules::MembershipEntity::Membership;
  10         36  
  10         453  
14 10     10   6451 use CMS::Drupal::Modules::MembershipEntity::Term;
  10         36  
  10         8782  
15              
16             has dbh => ( is => 'ro', isa => InstanceOf['DBI::db'], required => 1 );
17             has prefix => ( is => 'ro', isa => Maybe[StrMatch[ qr/ \w+_ /x ]] );
18              
19             sub fetch_memberships {
20              
21 17     17 1 84108 my $self = shift;
22              
23             ## We accept a list of mids as an optional parameter
24 17         44 my @mids = @_;
25              
26 17         41 my $WHERE = ' ';
27            
28 17 100       69 if ( @mids ) {
29 13 50       60 if ( scalar @mids < 1 ) {
30 0         0 carp 'Empty array passed to fetch_memberships() ... returning all Memberships';
31             }
32              
33 13 100       31 if ( ! grep { /all/ } @mids ) {
  35         106  
34             # ^ in that case no WHERE clause
35              
36 12         33 for ( @mids ) {
37             # Let's be real strict about what we try to pass in to the DBMS
38 34 100       197 croak 'FATAL: Invalid mid (must be all ASCII digits).'
39             unless /^[0-9]+$/;
40            
41 30         44 $WHERE = 'WHERE ';
42 30         229 $WHERE .= "mid = '$_' OR " for @mids;
43 30         121 $WHERE =~ s/ OR $//;
44             }
45             }
46             }
47              
48 13   50     97 my $prefix = ( $self->{'prefix'} || '' );
49              
50 13         26 my %temp;
51             my %memberships;
52              
53             ## Get the Membership info
54 13         47 my $sql = qq|
55             SELECT mid, member_id, type, uid, status, created, changed
56             FROM ${prefix}membership_entity
57             $WHERE
58             |;
59            
60 13         208 my $sth = $self->{'dbh'}->prepare( $sql );
61 13         2299 $sth->execute;
62            
63 13         148 my $results = $sth->fetchall_hashref('mid');
64 13         3637 foreach my $mid (keys( %{ $results } )) {
  13         62  
65 170         273 $temp{ $mid } = $results->{ $mid };
66             }
67            
68             ## Get the Membership Term info
69             # Use the $WHERE clause from the optional mids parameter
70 13         81 my $sql2 = qq|
71             SELECT id as tid, mid, status, term, modifiers, start, end
72             FROM ${prefix}membership_entity_term
73             $WHERE
74             ORDER BY start
75             |;
76            
77 13         89 my $sth2 = $self->{'dbh'}->prepare( $sql2 );
78 13         1534 $sth2->execute;
79              
80 13         27 my %term_count; # used to track array position of Terms
81              
82 13         334 while( my $row = $sth2->fetchrow_hashref ) {
83            
84             ## Shouldn't be, but is, possible to have a Term with no
85             ## start or end date
86 192 50 33     713 if ( not defined $row->{'start'} or not defined $row->{'end'} ) {
87 0         0 carp "MISSING DATE: tid[ $row->{'tid'} ] " .
88             "(uid[ $temp{ $row->{'mid'} }->{'uid'} ]) has no start " .
89             "or end date defined. Skipping ...";
90 0         0 next;
91             }
92              
93             ## Shouldn't be, but is, possible to have a Term with no
94             ## corresponding Memberships
95 192 50       584 if ( not defined $temp{ $row->{'mid'} } ) {
96 0         0 carp "TERM WITH NO MEMBERSHIP: tid[ $row->{'tid'} ] " .
97             "has no corresponding Membership. Skipping ...";
98 0         0 next;
99             }
100              
101             ## convert the start and end to unixtime
102 192         318 for (qw/ start end /) {
103 384         11966 my @datetime = reverse ( split /[-| |:]/, $row->{ $_ } );
104 384         738 $datetime[4]--;
105 384         950 $row->{ $_ } = timelocal( @datetime );
106             }
107              
108             ## Track which of the Membership's Terms this is
109 192         8414 $term_count{ $row->{'mid'} }++;
110 192         521 $row->{'array_position'} = $term_count{ $row->{'mid'} };
111            
112             ## Instantiate a MembershipEntity::Term object for each
113             ## Term now that we have the data
114 192         4367 my $term = CMS::Drupal::Modules::MembershipEntity::Term->new( $row );
115 192         54821 $temp{ $row->{'mid'} }->{'terms'}->{ $row->{'tid'} } = $term;
116             }
117              
118             ## Instantiate a MembershipEntity::Membership object for each
119             ## Membership now that we have the data
120 13         72 foreach my $mid(keys( %temp )) {
121            
122             ## Shouldn't be, but is, possible to have a Membership with no Term
123 170 50       38602 if (not defined $temp{ $mid }->{'terms'}) {
124 0         0 carp "MISSING TERM: mid[ $mid ] (uid[ $temp{ $mid }->{'uid'} ]) " .
125             "has no Membership Terms. Skipping ...";
126 0         0 next;
127             }
128              
129             $memberships{ $mid } =
130 170         3445 CMS::Drupal::Modules::MembershipEntity::Membership->new( $temp{ $mid } );
131             }
132            
133 13         11314 $self->{'_memberships'} = \%memberships;
134              
135             return (scalar keys %memberships == 1) ?
136 13 100       843 @memberships{ keys %memberships } :
137             \%memberships;
138             }
139              
140             1; ## return true to end package CMS::Drupal::Modules::MembershipEntity
141              
142             __END__