lib/CMS/Drupal/Modules/MembershipEntity.pm

filter: Statementstmt 90.7% 69 / 76 Branchbran 77.7% 14 / 18 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 84.8% 95 / 112 CC 17 SCAR 25.9
CC
17
Cov
86%
fetch_memberships
19.2
BEGIN
1.0
BEGIN
1.0
CRAP 13.4
« lib/CMS/Drupal/Modules/MembershipEntity/Stats.pm ↑ Summary lib/CMS/Drupal/Modules/MembershipEntity/Test.pm »
1 package CMS::Drupal::Modules::MembershipEntity;
2 10 $CMS::Drupal::Modules::MembershipEntity::VERSION = '0.96';
3 # ABSTRACT: Perl interface to Drupal MembershipEntity entities
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Types::Standard qw/ :all /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Carp qw/ carp croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use CMS::Drupal::Modules::MembershipEntity::Membership;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use CMS::Drupal::Modules::MembershipEntity::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 has dbh => ( is => 'ro', isa => InstanceOf['DBI::db'], required => 1 );
17 10 has prefix => ( is => 'ro', isa => Maybe[StrMatch[ qr/ \w+_ /x ]] );
18  
19 sub fetch_memberships {
20  
21 17 ▶ my $self = shift;
Subroutine fetch_memberships: called CC=17 SCAR=29.6
Pod documented
22  
23 ## We accept a list of mids as an optional parameter
24 17 my @mids = @_;
25  
26 17 my $WHERE = ' ';
27
28 17 ▶ if ( @mids ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@mids) 13 4 17
29 13 ▶ if ( scalar @mids < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @mids < 1) 0 13 13
30 0 carp 'Empty array passed to fetch_memberships() ... returning all Memberships';
31 }
32  
33 13 ▶ if ( ! grep { /all/ } @mids ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/all/;} @mids) 12 1 13
34 # ^ in that case no WHERE clause
35  
36 12 for ( @mids ) {
37 # Let's be real strict about what we try to pass in to the DBMS
38 34 ▶ croak 'FATAL: Invalid mid (must be all ASCII digits).'
Branch: 1 branch
BranchTrueFalseTotal
unless /^[0-9]+$/ 4 30 34
39 unless /^[0-9]+$/;
40
41 30 $WHERE = 'WHERE ';
42 30 $WHERE .= "mid = '$_' OR " for @mids;
43 30 $WHERE =~ s/ OR $//;
44 }
45 }
46 }
47  
48 13 ▶ my $prefix = ( $self->{'prefix'} || '' );
Condition50%1/2
$self->{'prefix'} || ''
l!l
013
MC/DC0%0/1
$self->{'prefix'} || ''
$self->{'prefix'}
Truth table50%1/2
A
A= $self->{'prefix'}
Aresult
11
00
49  
50 13 my %temp;
51 my %memberships;
52  
53 ## Get the Membership info
54 13 my $sql = qq|
55 SELECT mid, member_id, type, uid, status, created, changed
56 FROM ${prefix}membership_entity
57 $WHERE
58 |;
59
60 13 my $sth = $self->{'dbh'}->prepare( $sql );
61 13 $sth->execute;
62
63 13 my $results = $sth->fetchall_hashref('mid');
64 13 foreach my $mid (keys( %{ $results } )) {
65 170 $temp{ $mid } = $results->{ $mid };
66 }
67
68 ## Get the Membership Term info
69 # Use the $WHERE clause from the optional mids parameter
70 13 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 my $sth2 = $self->{'dbh'}->prepare( $sql2 );
78 13 $sth2->execute;
79  
80 13 my %term_count; # used to track array position of Terms
81  
82 13 ▶ while( my $row = $sth2->fetchrow_hashref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth2->fetchrow_hashref) 192 13 205
83
84 ## Shouldn't be, but is, possible to have a Term with no
85 ## start or end date
86 192 ▶ if ( not defined $row->{'start'} or not defined $row->{'end'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $row->{"start"} and defined $row->{"end"}) 0 192 192
Condition33%1/3
defined $row->{"start"} and defined $row->{"end"}
!ll&&!rl&&r
00192
MC/DC0%0/2
defined $row->{"start"} and defined $row->{"end"}
defined $row->{"start"} defined $row->{"end"}
Truth table33%1/3
A and B
A= defined $row->{"start"}B= defined $row->{"end"}
ABresult
0X0
100
111
87 0 carp "MISSING DATE: tid[ $row->{'tid'} ] " .
88 "(uid[ $temp{ $row->{'mid'} }->{'uid'} ]) has no start " .
89 "or end date defined. Skipping ...";
90 0 next;
91 }
92  
93 ## Shouldn't be, but is, possible to have a Term with no
94 ## corresponding Memberships
95 192 ▶ if ( not defined $temp{ $row->{'mid'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $temp{$row->{"mid"}}) 0 192 192
96 0 carp "TERM WITH NO MEMBERSHIP: tid[ $row->{'tid'} ] " .
97 "has no corresponding Membership. Skipping ...";
98 0 next;
99 }
100  
101 ## convert the start and end to unixtime
102 192 for (qw/ start end /) {
103 384 my @datetime = reverse ( split /[-| |:]/, $row->{ $_ } );
104 384 $datetime[4]--;
105 384 $row->{ $_ } = timelocal( @datetime );
106 }
107  
108 ## Track which of the Membership's Terms this is
109 192 $term_count{ $row->{'mid'} }++;
110 192 $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 my $term = CMS::Drupal::Modules::MembershipEntity::Term->new( $row );
115 192 $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 foreach my $mid(keys( %temp )) {
121
122 ## Shouldn't be, but is, possible to have a Membership with no Term
123 170 ▶ if (not defined $temp{ $mid }->{'terms'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $temp{$mid}{"terms"}) 0 170 170
124 0 carp "MISSING TERM: mid[ $mid ] (uid[ $temp{ $mid }->{'uid'} ]) " .
125 "has no Membership Terms. Skipping ...";
126 0 next;
127 }
128  
129 $memberships{ $mid } =
130 170 CMS::Drupal::Modules::MembershipEntity::Membership->new( $temp{ $mid } );
131 }
132
133 13 $self->{'_memberships'} = \%memberships;
134  
135 return (scalar keys %memberships == 1) ?
136 13 ▶ @memberships{ keys %memberships } :
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %memberships == 1 ? : 3 10 13
137 \%memberships;
138 }
139  
140 10 1; ## return true to end package CMS::Drupal::Modules::MembershipEntity
141  
142 __END__
143  
144 =pod
145  
146 =encoding UTF-8
147  
148 =head1 NAME
149  
150 CMS::Drupal::Modules::MembershipEntity - Perl interface to Drupal MembershipEntity entities
151  
152 =head1 VERSION
153  
154 version 0.96
155  
156 =head1 SYNOPSIS
157  
158 use CMS::Drupal::Modules::MembershipEntity;
159  
160 my $ME = CMS::Drupal::Modules::MembershipEntity->new( { dbh => $dbh } );
161  
162 my $href = $ME->fetch_memberships( 'all' );
163  
164 # or:
165 my $href = $ME->fetch_memberships( @list );
166  
167 foreach my $mid ( sort keys %{ $hashref } ) {
168 my $mem = $hashref->{ $mid };
169
170 print $mem->type;
171 send_newsletter( $mem->uid ) if $mem->active;
172
173 # etc ...
174 }
175  
176 Or, for a single Membership:
177  
178 my $mem = $ME->fetch_memberships( 123 );
179  
180 print $mem->type;
181 send_newsletter( $mem->uid ) if $mem->active;
182
183 # etc ...
184  
185 =head1 METHODS
186  
187 =head2 fetch_memberships
188  
189 This method returns either a hashref containing Membership objects indexed by
190 B<mid>, or a single Membership object (if it was called with a single B<mid>).
191  
192 When called with the argument 'all', the hashref contains all Memberships in
193 the Drupal database, which might be too much for your memory if you have
194 lots of them.
195  
196 When called with an array containing B<mid>s, the hashref will contain an
197 object for each mid in the array.
198  
199 When called with a single B<mid>, the method will return a single object
200 (no hashref).
201  
202 # Fetch a single Membership
203 my $mem = $ME->fetch_memberships( 1234 );
204  
205 # Fetch a set of Memberships
206 my $hashref = $ME->fetch_memberships( 1234, 5678 );
207  
208 # Fetch a set of Memberships using a list you prepared elsewhere
209 my $hashref = $ME->fetch_memberships( @list );
210  
211 # Fetch all your Memberships
212 my $hashref = $ME->fetch_memberships('all');
213  
214 # Same thing but with a warning
215 my $hashref = $ME->fetch_memberships();
216  
217 IMPORTANT: If you have bad records in your Drupal database, the module will
218 print a warning and skip the record. This happens when there are no Terms
219 belonging to the Membership, or when the Term is missing a start date or end
220 date. You should immediately normalize your data! This issue will also
221 cause installation testing to fail if you have configured your environment
222 to test against your real Drupal database.
223  
224 =head1 USAGE
225  
226 This package provides easy access to Perl objects representing Membership
227 Entity memberships and their terms. Rather than creating those objects
228 directly, you should allow this module to do so.
229  
230 For each Membership that you want, you can fetch a Membership object, which
231 contains at least one Term object, so you have access to all the methods
232 you can use on your Membership and its Terms.
233  
234 For this reason the methods actually provided by the submodules are documented
235 here.
236  
237 =head2 Memberships
238  
239 This module uses CMS::Drupal::Modules::MembershipEntity::Membership so you
240 don't have to. The methods shown below are actually in the latter
241 module where they are documented completely.
242  
243 =head3 Attributes
244  
245 You can directly access all the Membership's attributes as follows:
246  
247 $mem->attr_name
248  
249 Where attr_name is one of:
250  
251 mid
252 member_id
253 type
254 uid
255 status
256 created
257 changed
258  
259 There is also another attribute 'terms', which contains a hashref of Term
260 objects, indexed by B<tid>. Each Term can be accessed by the methods described
261 in the Membership Terms section below.
262  
263 =head3 Membership methods
264  
265 Once you have the Membership object, you can call some methods on it:
266  
267 print 'User ' . $mem->uid . ' is in good standing' if $mem->is_active;
268 print $mem->mid . ' has already renewed' if $mem->has_renewal;
269  
270 Methods are:
271  
272 =over 4
273  
274 =item *
275  
276 is_active()
277  
278 =item *
279  
280 is_expired()
281  
282 =item *
283  
284 is_cancelled()
285  
286 =item *
287  
288 is_pending()
289  
290 =item *
291  
292 has_renewal()
293  
294 =item *
295  
296 current_was_renewal()
297  
298 =back
299  
300 =head2 Membership Terms
301  
302 This module uses CMS::Drupal::Modules::MembershipEntity::Term so you
303 don't have to. The methods described below are actually in the latter
304 module where they are documented compeletely.
305  
306 while ( my ($tid, $term) = each %{ $mem->{'terms'} } ) {
307 # do something ...
308 }
309  
310 =head3 Attributes
311  
312 You can directly access all the Term's attributes as follows:
313  
314 $term->attr_name;
315  
316 Where attr_name is one of:
317  
318 tid
319 mid
320 status
321 term
322 modifiers
323 start
324 end
325  
326 There is also another attribute, 'array_position', which is used to determine if
327 the Term is a renewal, etc.
328  
329 =head3 Membership Term methods
330  
331 print 'This is a live one' if $term->is_current;
332 push @renewals, $term->tid if $term->was_renewal;
333  
334 Methods are:
335  
336 =over 4
337  
338 =item *
339  
340 is_active()
341  
342 =item *
343  
344 is_current()
345  
346 =item *
347  
348 is_future()
349  
350 =item *
351  
352 was_renewal()
353  
354 =back
355  
356 =head1 SEE ALSO
357  
358 =over 4
359  
360 =item *
361  
362 L<CMS::Drupal::Modules::MembershipEntity::Membership|CMS::Drupal::Modules::MembershipEntity::Membership>
363  
364 =item *
365  
366 L<CMS::Drupal::Modules::MembershipEntity::Term|CMS::Drupal::Modules::MembershipEntity::Term>
367  
368 =item *
369  
370 L<CMS::Drupal|CMS::Drupal>
371  
372 =back
373  
374 =head1 AUTHOR
375  
376 Nick Tonkin <tonkin@cpan.org>
377  
378 =head1 COPYRIGHT AND LICENSE
379  
380 This software is copyright (c) 2015 by Nick Tonkin.
381  
382 This is free software; you can redistribute it and/or modify it under
383 the same terms as the Perl 5 programming language system itself.
384  
385 =cut
« lib/CMS/Drupal/Modules/MembershipEntity/Stats.pm ↑ Summary lib/CMS/Drupal/Modules/MembershipEntity/Test.pm »