lib/Magpie/Resource/Kioku.pm

filter: Statementstmt 46.0% 81 / 176 Branchbran 27.5% 11 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 43.2% 16 / 37 Podpod 0.0% 0 / 3 totaltotal 41.7% 109 / 261 CC 26 SCAR 29.1
CC
26
Cov
42%
PUT
90.0
POST
19.0
DELETE
12.0
CRAP 18.3
« blib/lib/Magpie/Component/ContentCache.pm ↑ Summary blib/lib/Magpie/Matcher.pm »
1 package Magpie::Resource::Kioku;
2 2 $Magpie::Resource::Kioku::VERSION = '1.163200';
3 # ABSTRACT: INCOMPLETE - Resource implementation for KiokuDB datastores.
4  
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 extends 'Magpie::Resource';
7 2 with 'Magpie::Plugin::DBI';
8  
9 2 ▶use Magpie::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use KiokuDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 has data_source => (
15 is => 'ro',
16 isa => 'KiokuDB',
17 lazy_build => 1,
18 );
19  
20 2 has wrapper_class => (
21 isa => "Str",
22 is => "ro",
23 required => 1,
24 default => 'MagpieGenericWrapper',
25 );
26  
27 2 has typemap => (
28 isa => "KiokuDB::TypeMap",
29 is => "ro",
30 predicate => "has_typemap",
31 );
32  
33 2 has _kioku_scope => (
34 is => 'rw',
35 isa => 'KiokuDB::LiveObjects::Scope',
36 );
37  
38 3 ▶sub _build_data_source {
Subroutine _build_data_source: called CC=2 SCAR=7.3
39 3 my $self = shift;
40 3 my $k = undef;
41  
42 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 3 $k = $self->resolve_asset( service => 'kioku_dir' );
44 }
45 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=1.2
46 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 $k = KiokuDB->connect( @{ $self->_connect_args } );
48 }
49 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 my $error = "Could not connect to Kioku data source: $_\n";
51 0 warn $error;
52 0 $self->set_error( { status_code => 500, reason => $error } );
53 1 };
54 3 };
55  
56 3 ▶ return undef if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 3 3
57 3 $self->_kioku_scope( $k->new_scope );
58 3 return $k;
59 }
60  
61 2 ▶sub GET {
Subroutine GET: called CC=5 SCAR=18.6
Pod undocumented
62 2 my $self = shift;
63 2 my $ctxt = shift;
64 2 $self->parent_handler->resource($self);
65 2 my $req = $self->request;
66  
67 2 my $path = $req->path_info;
68 2 my $id = $self->get_entity_id($ctxt);
69  
70 2 ▶ if ($path =~ /\/$/ && !$id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/$] and not $id) 0 2 2
Condition33%1/3
$path =~ m[/$] and not $id
!ll&&!rl&&r
200
MC/DC0%0/2
$path =~ m[/$] and not $id
$path =~ m[/$] not $id
Truth table33%1/3
A and not(B)
A= $path =~ m[/$]B= not $id
ABresult
0X0
100
111
71 0 $self->state('prompt');
72 0 return OK;
73 }
74  
75 2 my $data = undef;
76  
77 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 2 ($data) = $self->data_source->lookup($id);
79 }
80 0 ▶ catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
81 0 my $trace = $_;
82 0 ▶ if (ref($trace)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trace) 0 0 0
83 0 $trace = $trace->as_string;
84 }
85 0 my $error = "Could not GET data from Kioku data source: $trace\n";
86 0 $self->set_error( { status_code => 500, reason => $error } );
87 2 };
88  
89 2 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 2 2
90  
91 2 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 1 2
92 1 $self->set_error({ status_code => 404, reason => 'Resource not found.'});
93 1 return OK;
94 }
95  
96 1 $self->data($data);
97 1 return OK;
98 }
99  
100 1 ▶sub POST {
Subroutine POST: called CC=10 SCAR=29.4
Pod undocumented
101 1 my $self = shift;
102 1 $self->parent_handler->resource($self);
103 1 my $req = $self->request;
104  
105 1 my $to_store = undef;
106  
107 1 my $wrapper_class = $self->wrapper_class;
108  
109 # XXX should check for a content body first.
110 1 my %args = ();
111  
112 1 ▶ if ( $self->has_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_data) { } 0 1 1
113 0 %args = %{ $self->data };
114 0 $self->clear_data;
115 }
116 else {
117 1 for ( $req->param ) {
118 2 $args{$_} = $req->param($_);
119 }
120 }
121  
122 # permit POST to update if there's an entity ID.
123 # XXX: Should this go in an optional Role?
124 1 ▶ if (my $existing_id = $self->get_entity_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing_id = $self->get_entity_id) 1 0 1
125 1 my $existing = undef;
126 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
127 1 ($existing) = $self->data_source->lookup($existing_id);
128 }
129 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 my $error = "Could not fetch data from Kioku data source for POST editing if entity with ID $existing_id: $_\n";
131 0 $self->set_error( { status_code => 500, reason => $error } );
132 1 };
133  
134 1 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
135  
136 1 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 0 1 1
137 0 foreach my $key (keys(%args)) {
138 0 $existing->$key( $args{$key} );
139 }
140  
141 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 ▶ $self->data_source->txn_do( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $self->data_source->store($existing);
144 0 });
145 }
146 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 my $error = "Error updating data entity with ID $existing_id: $_\n";
148 0 $self->set_error( { status_code => 500, reason => $error } );
149 0 };
150  
151 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
152  
153 # finally, if it all went OK, say so.
154 0 $self->state('updated');
155 0 $self->response->status(204);
156 0 return OK;
157 }
158 }
159  
160 # if we make it here there is no existing record, so make a new one.
161 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 1 Class::Load::load_class($wrapper_class);
163 1 $to_store = $wrapper_class->new(%args);
164 }
165 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 my $error
167 = "Could not create instance of wrapper class '$wrapper_class': $_\n";
168 0 warn $error;
169 0 $self->set_error( { status_code => 500, reason => $error } );
170 1 };
171  
172 1 ▶ return DECLINED if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
173  
174 1 my $id = undef;
175  
176 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 1 ▶ $self->data_source->txn_do( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
178 1 $id = $self->data_source->store($to_store);
179 1 });
180 }
181 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 0 my $error = "Could not store POST data in Kioku data source: $_\n";
183 0 warn $error;
184 0 $self->set_error( { status_code => 500, reason => $error } );
185 1 };
186  
187 1 ▶ return DECLINED if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 1 1
188  
189 # XXX: all of this needs to go in an abstract object downstream serializer
190 # can figure stuff out
191 1 my $path = $req->path_info;
192 1 $path =~ s|^/||;
193 1 $path =~ s|/$||;
194 1 $self->state('created');
195 1 $self->response->status(201);
196 1 $self->response->header( 'Location' => $req->base . $path . "/$id" );
197 1 return OK;
198 }
199  
200 0 ▶sub DELETE {
Subroutine DELETE: not called CC=3 SCAR=24.8
201 0 my $self = shift;
202 0 my $ctxt = shift;
203 0 $self->parent_handler->resource( $self );
204 0 my $req = $self->request;
205  
206 0 my $path = $req->path_info;
207  
208 0 ▶ if ( $path =~ /\/$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[/$]) 0 0 0
209 0 $self->state('prompt');
210 0 return OK;
211 }
212  
213 0 my $id = $self->get_entity_id($ctxt);
214  
215 # should we do a separate lookup to make sure the data is there?
216 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 ▶ $self->data_source->txn_do( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 $self->data_source->delete( $id );
219 0 });
220 }
221 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 my $error = "Could not delete data from Kioku data source: $_\n";
223 0 $self->set_error({ status_code => 500, reason => $error });
224 0 };
225  
226 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
227 0 $self->state('deleted');
228 0 $self->response->status(204);
229 0 return OK;
230 }
231  
232  
233 0 ▶sub PUT {
Subroutine PUT: not called CC=9 SCAR=45.0
Pod undocumented
234 0 my $self = shift;
235 0 $self->parent_handler->resource($self);
236 0 my $req = $self->request;
237  
238 0 my $to_store = undef;
239  
240 0 my $wrapper_class = $self->wrapper_class;
241  
242 # XXX should check for a content body first.
243 0 my %args = ();
244  
245 0 ▶ if ( $self->has_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_data) { } 0 0 0
246 0 %args = %{ $self->data };
247 0 $self->clear_data;
248 }
249 else {
250 0 for ( $req->param ) {
251 0 $args{$_} = $req->param($_);
252 }
253 }
254  
255 0 my $existing_id = $self->get_entity_id;
256  
257 0 ▶ unless ($existing_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing_id) 0 0 0
258 0 $self->set_error({
259 status_code => 400,
260 reason => "Attempt to PUT without a definable entity ID."
261 });
262 0 return DONE;
263 }
264  
265  
266 0 my $existing = undef;
267 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 ($existing) = $self->data_source->lookup($existing_id);
269 }
270 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
271 0 my $error = "Could not fetch data from Kioku data source for PUT editing if entity with ID $existing_id: $_\n";
272 0 $self->set_error( { status_code => 500, reason => $error } );
273 0 };
274  
275 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
276  
277 0 ▶ unless ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing) 0 0 0
278 0 $self->set_error(404);
279 0 return DONE;
280 }
281  
282 0 foreach my $key (keys(%args)) {
283 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
284 0 $existing->$key( $args{$key} );
285 }
286 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 0 my $error = "Error updating property '$key' of Resource ID $existing_id: $_\n";
288 0 $self->set_error( { status_code => 500, reason => $error } );
289 0 last;
290 0 };
291 }
292  
293  
294 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
295  
296 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
297 0 ▶ $self->data_source->txn_do(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my $scope = $self->data_source->new_scope;
299 0 $self->data_source->store($existing);
300 0 });
301 }
302 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
303 0 my $error = "Error updating data entity with ID $existing_id: $_\n";
304 0 $self->set_error( { status_code => 500, reason => $error } );
305 0 };
306  
307 0 ▶ return OK if $self->has_error;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_error 0 0 0
308  
309 # finally, if it all went OK, say so.
310 0 $self->state('updated');
311 0 $self->response->status(204);
312 0 return OK;
313 }
314  
315  
316 package MagpieGenericWrapper;
317 2 $MagpieGenericWrapper::VERSION = '1.163200';
318 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
319 0 my $proto = shift;
320 0 my %args = @_;
321 0 return bless \%args, $proto;
322 }
323  
324 2 1;
325  
326 __END__
« blib/lib/Magpie/Component/ContentCache.pm ↑ Summary blib/lib/Magpie/Matcher.pm »