File Coverage

blib/lib/Connector/Role/LocalPath.pm
Criterion Covered Total %
statement 22 28 78.5
branch 6 12 50.0
condition 2 4 50.0
subroutine 4 4 100.0
pod n/a
total 34 48 70.8


line stmt bran cond sub pod time code
1             use Moose::Role;
2 4     4   3416 use Template;
  4         13639  
  4         23  
3 4     4   19122 use utf8;
  4         35  
  4         102  
4 4     4   1717  
  4         44  
  4         30  
5             requires 'DELIMITER';
6              
7             has file => (
8             is => 'rw',
9             isa => 'Str',
10             );
11              
12             has path => (
13             is => 'rw',
14             isa => 'Str',
15             );
16              
17              
18              
19             my $self = shift;
20             my $args = shift || [];
21 21     21   39 my $data = shift;
22 21   50     73  
23 21         36 my $file;
24             if ($self->path()) {
25 21         29 my $pattern = $self->path();
26 21 50 50     592 $self->log()->debug('Process template ' . $pattern);
    100          
    50          
27 0         0 Template->new({})->process( \$pattern, { ARGS => $args, DATA => $data }, \$file) || die "Error processing file template.";
28 0         0 # yes we allow paths here
29 0 0       0 $file =~ s/[^\s\w\.\-\/\\]//g;
30             } elsif ($self->file()) {
31 0         0 my $pattern = $self->file();
32             $self->log()->debug('Process template ' . $pattern);
33 19         414 Template->new({})->process( \$pattern, { ARGS => $args, DATA => $data }, \$file) || die "Error processing argument template.";
34 19         341 if ($file =~ m{[^\s\w\.\-]}) {
35 19 50       236 $self->log()->error('Target file name contains disallowed characters! Consider using path instead.');
36 19 50       129467 die "Target file name contains disallowed seperator! Consider using path instead.";
37 0         0 }
38 0         0 } elsif (ref $args && scalar @{$args}) {
39             $file = join $self->DELIMITER(), @{$args};
40 2         5 }
41 2         38  
  2         7  
42             return $file;
43             }
44 21         732  
45             1;
46              
47              
48              
49             =head1 Connector::Role::LocalPath
50              
51             This role is used to generate file or pathnames from a template string
52             and the path arguments given. It also accepts additional data as a hash.
53              
54             =head2 Parameters
55              
56             =over
57              
58             =item file
59              
60             A template toolkit string to generate the filename to write to. The
61             arguments given as connector location are available in I<ARGS>, the
62             payload data in I<DATA>. The class will die if the result contains
63             any other characters than word, whitespace, dash or dot.
64              
65             =item path
66              
67             Similar to file with slash and backslash also allowed. Disallowed
68             characters will be removed and the sanitized string is returned.
69              
70             =back