Lighthouse/Project/TicketBin.pm

filter: Statementstmt 53.4% 46 / 86 Branchbran 33.3% 6 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 7 / 10 Podpod 100% 6 / 6 totaltotal 54.1% 65 / 120 CC 11 SCAR 14.3
CC
11
Cov
50%
create
6.0
update
6.0
delete
6.0
CRAP 4.2
« Lighthouse/User.pm ↑ Summary Lighthouse/Project/Milestone.pm »
1 package Net::Lighthouse::Project::TicketBin;
2 11 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use Params::Validate ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Net::Lighthouse::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 extends 'Net::Lighthouse::Base';
6  
7 # read only attr
8 11 has 'updated_at' => (
9 isa => 'DateTime',
10 is => 'ro',
11 );
12  
13 11 has [ 'user_id', 'position', 'project_id', 'tickets_count', 'id' ] => (
14 isa => 'Int',
15 is => 'ro',
16 );
17  
18 11 has 'shared' => (
19 isa => 'Bool',
20 is => 'ro',
21 );
22  
23 # read&write attr
24 11 has 'default' => (
25 isa => 'Bool',
26 is => 'rw',
27 );
28  
29 11 has [qw/name query/] => (
30 isa => 'Maybe[Str]',
31 is => 'rw',
32 );
33  
34 11 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 11 __PACKAGE__->meta->make_immutable;
36  
37 1 ▶sub load {
Subroutine load: called CC=4 SCAR=17.5
Pod documented
38 1 my $self = shift;
39 1 validate_pos( @_, { type => SCALAR, regex => qr/^\d+|\w+$/ } );
40 1 my $id = shift;
41  
42 1 ▶ if ( $id !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id =~ /^\d+$/) 0 1 1
43  
44 # so we got a name, let's find it
45 0 my ($bin) = grep { $_->name eq $id } $self->list;
46 0 ▶ if ($bin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin) { } 0 0 0
47 0 $id = $bin->id;
48 }
49 else {
50 0 die "can't find ticket bin $id in project "
51 . $self->project_id
52 . ' in account '
53 . $self->account;
54 }
55 }
56  
57 1 my $ua = $self->ua;
58 1 my $url =
59 $self->base_url
60 . '/projects/'
61 . $self->project_id
62 . '/bins/'
63 . $id . '.xml';
64 1 my $res = $ua->get($url);
65 1 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 1 0 1
66 1 $self->load_from_xml( $res->content );
67 }
68 else {
69 0 die "try to get $url failed: "
70 . $res->status_line . "\n"
71 . $res->content;
72 }
73 }
74  
75 10 ▶sub load_from_xml {
Subroutine load_from_xml: called CC=2 SCAR=6.9
Pod documented
76 10 my $self = shift;
77 10 my $ref = Net::Lighthouse::Util->translate_from_xml(shift);
78  
79 # dirty hack: some attrs are read-only, and Mouse doesn't support
80 # writer => '...'
81 10 for my $k ( keys %$ref ) {
82 100 $self->{$k} = $ref->{$k};
83 }
84 10 return $self;
85 }
86  
87 0 ▶sub create {
Subroutine create: not called CC=2 SCAR=17.9
Pod documented
88 0 my $self = shift;
89 0 validate(
90 @_,
91 {
92 name => { type => SCALAR },
93 query => { type => SCALAR },
94 default => { optional => 1, type => BOOLEAN },
95 }
96 );
97 0 my %args = @_;
98  
99 0 my $xml = Net::Lighthouse::Util->translate_to_xml(
100 \%args,
101 root => 'bin',
102 boolean => ['default'],
103 );
104  
105 0 my $ua = $self->ua;
106  
107 0 my $url = $self->base_url . '/projects/' . $self->project_id . '/bins.xml';
108  
109 0 my $request = HTTP::Request->new( 'POST', $url, undef, $xml );
110 0 my $res = $ua->request($request);
111 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
112 0 $self->load_from_xml( $res->content );
113 0 return 1;
114 }
115 else {
116 0 die "try to POST $url failed: "
117 . $res->status_line . "\n"
118 . $res->content;
119 }
120 }
121  
122 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
123 0 my $self = shift;
124 0 validate(
125 @_,
126 {
127 name => { optional => 1, type => SCALAR },
128 query => { optional => 1, type => SCALAR },
129 default => { optional => 1, type => BOOLEAN },
130 }
131 );
132 0 my %args = ( ( map { $_ => $self->$_ } qw/name query default/ ), @_ );
133  
134 0 my $xml = Net::Lighthouse::Util->translate_to_xml(
135 \%args,
136 root => 'bin',
137 boolean => ['default'],
138 );
139  
140 0 my $ua = $self->ua;
141 0 my $url =
142 $self->base_url
143 . '/projects/'
144 . $self->project_id
145 . '/bins/'
146 . $self->id . '.xml';
147  
148 0 my $request = HTTP::Request->new( 'PUT', $url, undef, $xml );
149 0 my $res = $ua->request($request);
150 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
151 0 $self->load( $self->id ); # let's reload
152 0 return 1;
153 }
154 else {
155 0 die "try to PUT $url failed: "
156 . $res->status_line . "\n"
157 . $res->content;
158 }
159 }
160  
161 0 ▶sub delete {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
162 0 my $self = shift;
163 0 my $ua = $self->ua;
164 0 my $url =
165 $self->base_url
166 . '/projects/'
167 . $self->project_id
168 . '/bins/'
169 . $self->id . '.xml';
170  
171 0 my $request = HTTP::Request->new( 'DELETE', $url );
172 0 my $res = $ua->request($request);
173 0 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
174 0 return 1;
175 }
176 else {
177 0 die "try to DELETE $url failed: "
178 . $res->status_line . "\n"
179 . $res->content;
180 }
181 }
182  
183 3 ▶sub list {
Subroutine list: called CC=4 SCAR=14.0
Pod documented
184 3 my $self = shift;
185 3 my $url = $self->base_url . '/projects/' . $self->project_id . '/bins.xml';
186 3 my $ua = $self->ua;
187 3 my $res = $ua->get($url);
188 3 ▶ if ( $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 3 0 3
189 my $bs =
190 3 Net::Lighthouse::Util->read_xml( $res->content )->{'ticket-bins'}{'ticket-bin'};
191 my @list = map {
192 3 my $t = Net::Lighthouse::Project::TicketBin->new(
193 27 map { $_ => $self->$_ }
194 9 grep { $self->$_ } qw/account auth project_id/
195 );
196 9 ▶ $t->load_from_xml($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $bs eq 'ARRAY' ? : 3 0 3
197 } ref $bs eq 'ARRAY' ? @$bs : $bs;
198 3 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
199 }
200 else {
201 0 die "try to get $url failed: "
202 . $res->status_line . "\n"
203 . $res->content;
204 }
205  
206 }
207  
208 11 1;
209  
210 __END__
« Lighthouse/User.pm ↑ Summary Lighthouse/Project/Milestone.pm »