File Coverage

blib/lib/OTRS/OPM/Installer/Utils/File.pm
Criterion Covered Total %
statement 34 36 94.4
branch n/a
condition n/a
subroutine 12 12 100.0
pod n/a
total 46 48 95.8


line stmt bran cond sub pod time code
1             package OTRS::OPM::Installer::Utils::File;
2             $OTRS::OPM::Installer::Utils::File::VERSION = '0.02';
3             # ABSTRACT: File related utility functions
4              
5 9     9   501455 use strict;
  9         101  
  9         285  
6 9     9   57 use warnings;
  9         24  
  9         303  
7              
8 9     9   3227 use File::HomeDir;
  9         49227  
  9         697  
9 9     9   104 use File::Spec;
  9         25  
  9         221  
10 9     9   5220 use File::Temp;
  9         170634  
  9         748  
11 9     9   4180 use HTTP::Tiny;
  9         336794  
  9         357  
12 9     9   2897 use IO::All;
  9         72877  
  9         67  
13 9     9   3500 use Moo;
  9         59557  
  9         95  
14 9     9   11355 use OTRS::OPM::Installer::Logger;
  9         32  
  9         291  
15 9     9   3485 use OTRS::OPM::Installer::Utils::Config;
  9         31  
  9         287  
16 9     9   2216 use OTRS::OPM::Installer::Types;
  9         24  
  9         260  
17 9     9   2705 use OTRS::Repository;
  0            
  0            
18             use Regexp::Common qw(URI);
19             use Types::Standard qw(ArrayRef Str);
20              
21             our $ALLOWED_SCHEME = 'HTTP';
22              
23             has repositories => ( is => 'ro', isa => ArrayRef[Str], default => \&_repository_list );
24             has package => ( is => 'ro', isa => Str, required => 1 );
25             has otrs_version => ( is => 'ro', isa => Str, required => 1 );
26             has version => ( is => 'ro', isa => Str );
27             has logger => ( is => 'ro', default => sub{ OTRS::OPM::Installer::Logger->new } );
28             has rc_config => ( is => 'ro', lazy => 1, default => \&_rc_config );
29             has conf => ( is => 'ro' );
30              
31             sub resolve_path {
32             my ($self) = @_;
33              
34             my $path;
35              
36             my $package = $self->package;
37             if ( $self->_is_url( $package ) ) {
38             # download file
39             $path = $self->_download( $package );
40             }
41             elsif ( -f $package ) {
42             # do nothing, file already exists
43             $path = $package;
44             }
45             else {
46             my @repositories = @{ $self->repositories || [] };
47              
48             for my $repo ( @repositories ) {
49             $repo .= '/otrs.xml' if '/otrs.xml' ne substr $repo, -9;
50             }
51              
52             my $repo = OTRS::Repository->new(
53             sources => \@repositories,
54             );
55              
56             my ($otrs) = $self->otrs_version =~ m{\A(\d+\.\d+)};
57              
58             my ($url) = $repo->find(
59             name => $package,
60             otrs => $otrs,
61             version => $self->version,
62             );
63              
64             return if !$url;
65              
66             $path = $self->_download( $url );
67             }
68              
69             return $path;
70             }
71              
72             sub _repository_list {
73             my ($self) = @_;
74              
75             my $config = $self->rc_config;
76             my $repositories = $config->{repository};
77              
78             return [] if !$repositories;
79             return $repositories;
80             }
81              
82             sub _is_url {
83             my ($self, $package) = @_;
84              
85             return $package =~ m{\A$RE{URI}{$ALLOWED_SCHEME}\z};
86             }
87              
88             sub _download {
89             my ($self, $url) = @_;
90              
91             my $file = File::Temp->new->filename;
92             my $response = HTTP::Tiny->new->mirror( $url, $file );
93              
94             $self->logger->notice( area => 'download', file => $file, success => $response->{success} );
95              
96             return $file;
97             }
98              
99             sub _rc_config {
100             my ($self) = @_;
101              
102             my $utils = OTRS::OPM::Installer::Utils::Config->new( conf => $self->conf );
103             my $config = $utils->rc_config;
104              
105             return $config;
106             }
107              
108             1;
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             OTRS::OPM::Installer::Utils::File - File related utility functions
119              
120             =head1 VERSION
121              
122             version 0.02
123              
124             =head1 AUTHOR
125              
126             Renee Baecker <reneeb@cpan.org>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is Copyright (c) 2017 by Renee Baecker.
131              
132             This is free software, licensed under:
133              
134             The Artistic License 2.0 (GPL Compatible)
135              
136             =cut