File Coverage

lib/Archive/BagIt/Role/OpenSSL.pm
Criterion Covered Total %
statement 38 49 77.5
branch 3 8 37.5
condition n/a
subroutine 11 12 91.6
pod 1 1 100.0
total 53 70 75.7


line stmt bran cond sub pod time code
1             package Archive::BagIt::Role::OpenSSL;
2 7     7   2857 use strict;
  7         13  
  7         210  
3 7     7   36 use warnings;
  7         22  
  7         217  
4 7     7   2365 use Archive::BagIt::Role::OpenSSL::Sync;
  7         16  
  7         209  
5 7     7   42 use Class::Load qw(load_class);
  7         12  
  7         293  
6 7     7   34 use Carp qw(carp);
  7         9  
  7         223  
7 7     7   30 use Moo::Role;
  7         9  
  7         35  
8 7     7   2284 use namespace::autoclean;
  7         11  
  7         30  
9             # ABSTRACT: A role that handles plugin loading
10             our $VERSION = '0.092'; # VERSION
11              
12              
13             has 'async_support' => (
14             is => 'ro',
15             builder => '_check_async_support',
16             predicate => 1,
17             lazy => 1,
18             );
19              
20             sub _check_async_support {
21 0     0   0 my $self = shift;
22 0 0       0 if (! exists $INC{'IO/Async.pm'}) {
23 0         0 carp "Module 'IO::Async' not available, disable async support";
24 0         0 $self->bagit->use_async(0);
25 0         0 return 0;
26             }
27 0         0 load_class('IO::Async');
28 0         0 return 1;
29             }
30              
31              
32              
33             sub _get_hash_string_sync {
34 549     549   776 my ($self, $fh, $blksize)=@_;
35 549         8631 my $obj = Archive::BagIt::Role::OpenSSL::Sync->new( name => $self->name);
36 549         14028 return $obj->calc_digest($fh, $blksize);
37             }
38              
39             sub _get_hash_string_async {
40 10     10   19 my ($self, $fh, $blksize) = @_;
41 10         11 my $result;
42 10 50       21 if ($self->has_async_support()) {
43 0         0 my $class = 'Archive::BagIt::Role::OpenSSL::Async';
44 0 0       0 load_class($class) or croak("could not load class $class");
45 0         0 my $obj = $class->new(name => $self->name);
46 0         0 $result = $obj->calc_digest($fh, $blksize);
47             } else {
48 10         17 $result = $self->_get_hash_string_sync($fh, $blksize);
49             }
50 10         32 return $result;
51             }
52              
53              
54             sub get_hash_string {
55 549     549 1 896 my ($self, $fh) = @_;
56 549         1212 my $blksize = $self->get_optimal_bufsize($fh);
57 549         1177 my $bagobj = $self->bagit;
58 549 100       10042 if ($bagobj->use_async) {
59 10         66 return $self->_get_hash_string_async($fh, $blksize);
60             }
61 539         3326 return $self->_get_hash_string_sync($fh, $blksize);
62             }
63              
64              
65 7     7   2772 no Moo;
  7         14  
  7         23  
66             1;
67              
68             __END__