lib/WebService/Braintree/ResourceCollection.pm

filter: Statementstmt 81.8% 36 / 44 Branchbran 50.0% 5 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 75.0% 9 / 12 Podpod 62.5% 5 / 8 totaltotal 72.7% 56 / 77 CC 8 SCAR 10.8
CC
8
Cov
75%
is_success
6.0
first
6.0
each
4.0
CRAP 3.0
« lib/WebService/Braintree/PaymentMethodNonceGateway.pm ↑ Summary lib/WebService/Braintree/ApplePayGateway.pm »
1 # vim: sw=4 ts=4 ft=perl
2  
3 package WebService::Braintree::ResourceCollection;
4 22 $WebService::Braintree::ResourceCollection::VERSION = '1.7';
5 22 ▶use 5.010_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 WebService::Braintree::ResourceCollection
11  
12 =head1 PURPOSE
13  
14 This class provides a way of iterating over a collection of resources. It will
15 lazily retrieve the objects from Braintree.
16  
17 This class will only be created as part of a L<response|WebService::Braintree::Result> or L<error response|WebService::Braintree::ErrorResult>.
18  
19 =cut
20  
21 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 22 ▶use Types::Standard qw(CodeRef HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # These attributes and methods are undocumented because they're meant to be
26 # internal-use only. But, prepending underscores makes the code ugly.
27  
28 22 has callback => (
29 is => 'ro',
30 isa => CodeRef,
31 required => 1,
32 );
33 22 has response => (
34 is => 'ro',
35 isa => HashRef,
36 required => 1,
37 );
38  
39 12 ▶sub ids {
Subroutine ids: called CC=1 SCAR=0.0
Pod undocumented
40 12 my $self = shift;
41 12 return $self->response->{search_results}{ids};
42 }
43  
44 5 ▶sub page_size {
Subroutine page_size: called CC=1 SCAR=0.0
Pod undocumented
45 5 my $self = shift;
46 5 return $self->response->{search_results}{page_size};
47 }
48  
49 =head1 METHODS
50  
51 =cut
52  
53 =head2 is_success
54  
55 This returns true if the request was successful, otherwise false.
56  
57 =cut
58  
59 0 ▶sub is_success {
Subroutine is_success: not called CC=2 SCAR=17.9
Pod documented
60 0 my $self = shift;
61 0 ▶ return 1 unless $self->response->{api_error_response};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->response->{"api_error_response"} 0 0 0
62 0 return 0;
63 }
64  
65 =head2 first
66  
67 This returns the first object in this collection.
68  
69 If there is nothing in the collection, this returns undef.
70  
71 =cut
72  
73 0 ▶sub first {
Subroutine first: not called CC=2 SCAR=17.9
Pod documented
74 0 my $self = shift;
75 0 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 0 0
76 0 return $self->callback->([$self->ids->[0]])->[0];
77 }
78  
79 =head2 is_empty
80  
81 This returns true if the collection has anything in it, otherwise false.
82  
83 =cut
84  
85 0 ▶sub is_empty {
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod documented
86 0 my $self = shift;
87 0 $self->maximum_size == 0;
88 }
89  
90 =head2 maximum_size
91  
92 This returns the number of elements in this collection.
93  
94 =cut
95  
96 7 ▶sub maximum_size {
Subroutine maximum_size: called CC=2 SCAR=7.1
Pod documented
97 7 my $self = shift;
98 7 ▶ return (scalar @{$self->ids || []});
Condition50%1/2
$self->ids || []
l!l
70
MC/DC0%0/1
$self->ids || []
$self->ids
Truth table50%1/2
A
A= $self->ids
Aresult
11
00
99 }
100  
101 =head2 each($block)
102  
103 This takes a subroutine and executes that subroutine for each element in this
104 collection.
105  
106 =cut
107  
108 1 ▶sub each {
Subroutine each: called CC=4 SCAR=13.9
Pod documented
109 1 my ($self, $block) = @_;
110  
111 1 my @page = ();
112 1 ▶ for (my $count = 0; $count < $self->maximum_size; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < $self->maximum_size) 5 1 6
113 5 push(@page, $self->ids->[$count]);
114 5 ▶ if ((scalar @page) == $self->page_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @page == $self->page_size) 2 3 5
115 2 $self->execute_block_for_page($block, \@page);
116 2 @page = ();
117 }
118 }
119  
120 1 ▶ $self->execute_block_for_page($block, \@page) if @page;
Branch: 1 branch
BranchTrueFalseTotal
if @page 1 0 1
121 1 return;
122 }
123  
124 3 ▶sub execute_block_for_page {
Subroutine execute_block_for_page: called CC=2 SCAR=6.9
Pod undocumented
125 3 my ($self, $block, $page) = @_;
126 3 $block->($_) for @{$self->callback->($page)};
127 3 return;
128 }
129  
130 22 __PACKAGE__->meta->make_immutable;
131  
132 22 1;
133 __END__
« lib/WebService/Braintree/PaymentMethodNonceGateway.pm ↑ Summary lib/WebService/Braintree/ApplePayGateway.pm »