FreeIPA/Response.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 8 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 95.0% 76 / 80 CC 8 SCAR 9.1
CC
8
Cov
97%
set_result
5.0
new
3.0
is_error
2.0
CRAP 2.5
« FreeIPA/Request.pm ↑ Summary FreeIPA/DummyLogger.pm »
1 package Net::FreeIPA::Response;
2 8 $Net::FreeIPA::Response::VERSION = '3.0.3';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Net::FreeIPA::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our @EXPORT = qw(mkresponse);
11  
12 8 ▶use overload bool => '_boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 Readonly my $RESULT_PATH => 'result/result';
17  
18 =head1 NAME
19  
20 Net::FreeIPA::Response is an response class for Net::FreeIPA.
21  
22 Boolean logic is overloaded using C<_boolean> method (as inverse of C<is_error>).
23  
24 =head2 Public methods
25  
26 =over
27  
28 =item mkresponse
29  
30 A C<Net::FreeIPA::Response> factory
31  
32 =cut
33  
34 sub mkresponse
35 4 ▶{
Subroutine mkresponse: called CC=1 SCAR=0.0
Pod documented
36 4 return Net::FreeIPA::Response->new(@_);
37 }
38  
39  
40 =item new
41  
42 Create new response instance.
43  
44 Options
45  
46 =over
47  
48 =item answer: complete answer hashref
49  
50 =item error: an error (passed to C<mkerror>).
51  
52 =item result_path: passed to C<set_result> to set the result attribute.
53  
54 =back
55  
56 =cut
57  
58 sub new
59 5 ▶{
Subroutine new: called CC=3 SCAR=11.1
Pod documented
60 5 my ($this, %opts) = @_;
61 5 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
62 my $self = {
63 answer => $opts{answer} || {},
64 5 ▶ };
Condition100%2/2
$opts{'answer'} || {}
l!l
23
MC/DC100%1/1
$opts{'answer'} || {}
$opts{'answer'}
Truth table100%2/2
A
A= $opts{'answer'}
Aresult
11
00
65 5 bless $self, $class;
66  
67 # First error
68 5 $self->set_error($opts{error});
69 # Then result
70 5 $self->set_result($opts{result_path});
71  
72 5 return $self;
73 };
74  
75 =item set_error
76  
77 Set and return the error attribute using C<mkerror>.
78  
79 =cut
80  
81 sub set_error
82 7 ▶{
Subroutine set_error: called CC=1 SCAR=0.0
Pod documented
83 7 my $self = shift;
84 7 $self->{error} = mkerror(@_);
85 7 return $self->{error};
86 }
87  
88 =item set_result
89  
90 Set and return the result attribute based on the C<result_path>.
91  
92 The C<result_path> is path-like string, indicating which subtree of the answer
93 should be set as result attribute (default C<result/result>).
94  
95 =cut
96  
97 sub set_result
98 9 ▶{
Subroutine set_result: called CC=5 SCAR=16.1
Pod documented
99 9 my ($self, $result_path) = @_;
100  
101 9 my $res;
102  
103 9 ▶ if (! $self->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 6 3 9
104 6 ▶ $result_path = $RESULT_PATH if ! defined($result_path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result_path 4 2 6
105  
106 6 $res = $self->{answer};
107 # remove any "empty" paths
108 6 foreach my $subpath (grep {$_} split('/', $result_path)) {
109 14 ▶ $res = $res->{$subpath} if (defined($res));
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 12 2 14
110 };
111 };
112  
113 9 $self->{result} = $res;
114  
115 9 return $self->{result};
116 };
117  
118 =item is_error
119  
120 Test if this is an error or not (based on error attribute).
121  
122 =cut
123  
124 sub is_error
125 18 ▶{
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
126 18 my $self = shift;
127 18 ▶ return $self->{error} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 8 10 18
128 }
129  
130 # Overloaded boolean, inverse of is_error
131 sub _boolean
132 4 ▶{
Subroutine _boolean: called CC=1 SCAR=0.0
133 4 my $self = shift;
134 4 return ! $self->is_error();
135 }
136  
137 =pod
138  
139 =back
140  
141 =cut
142  
143 8 1;
« FreeIPA/Request.pm ↑ Summary FreeIPA/DummyLogger.pm »