File Coverage

blib/lib/PayProp/API/Public/Client/Role/Storage.pm
Criterion Covered Total %
statement 15 35 42.8
branch 1 8 12.5
condition 1 5 20.0
subroutine 5 15 33.3
pod 4 4 100.0
total 26 67 38.8


line stmt bran cond sub pod time code
1             package PayProp::API::Public::Client::Role::Storage;
2              
3 3     3   370211 use strict;
  3         11  
  3         128  
4 3     3   19 use warnings;
  3         11  
  3         210  
5              
6 3     3   1595 use Mouse::Role;
  3         4907  
  3         14  
7             with qw/ PayProp::API::Public::Client::Role::Encrypt /;
8              
9 3     3   3359 use PayProp::API::Public::Client::Exception::Storage;
  3         10  
  3         1651  
10              
11             has cache_prefix => (
12             is => 'ro',
13             isa => 'Str',
14             lazy => 1,
15             default => sub { 'PayPropAPIToken_' }
16             );
17              
18             has cache_ttl_in_seconds => (
19             is => 'ro',
20             isa => 'Int',
21             lazy => 1,
22             default => sub { 3600 * 12 } # TTL: 12 hours
23             );
24              
25             has throw_on_storage_unavailable => ( is => 'ro', isa => 'Bool', lazy => 1, default => sub { 0 } );
26              
27             requires qw/
28             _set_p
29             _get_p
30             _ping_p
31             _delete_p
32             /;
33              
34             sub ping_p {
35 0     0 1 0 my ( $self ) = @_;
36              
37             return $self
38             ->_ping_p
39 0     0   0 ->catch( sub { $self->_handle_exception( @_ ) } )
40 0         0 ;
41             }
42              
43             sub set_p {
44 0     0 1 0 my ( $self, $key, $token ) = @_;
45              
46 0 0 0     0 die 'key and token are required for set_p' unless $key && $token;
47              
48             return $self
49             ->encrypt_hex_p( $token )
50             ->then( sub {
51 0     0   0 my ( $encrypted_token ) = @_;
52              
53 0         0 return $self->_set_p( $key, $encrypted_token );
54             } )
55 0     0   0 ->catch( sub { $self->_handle_exception( @_ ) } )
56 0         0 ;
57             }
58              
59             sub get_p {
60 0     0 1 0 my ( $self, $key ) = @_;
61              
62 0 0       0 die 'key is required for get_p' unless $key;
63              
64             return $self
65             ->_get_p( $key )
66 0     0   0 ->then( sub { $self->decrypt_hex_p( @_ ) } )
67 0     0   0 ->catch( sub { $self->_handle_exception( @_ ) } )
68 0         0 ;
69             }
70              
71             sub delete_p {
72 0     0 1 0 my ( $self, $key ) = @_;
73              
74 0 0       0 die 'key is required for delete_p' unless $key;
75              
76             return $self
77             ->_delete_p( $key )
78 0     0   0 ->catch( sub { $self->_handle_exception( @_ ) } )
79 0         0 ;
80             }
81              
82             sub _handle_exception {
83 1     1   1364 my ( $self, $exception ) = @_;
84              
85 1   50     6 $exception //= 'UNKKNOWN STORAGE ERROR';
86              
87             # stop promise chain
88 1 50       17 PayProp::API::Public::Client::Exception::Storage->throw("$exception")
89             if $self->throw_on_storage_unavailable
90             ;
91              
92 0           warn "$exception";
93              
94 0           return undef; # continue promise chain
95             }
96              
97             1;
98              
99             __END__