File Coverage

blib/lib/OTRS/Repository.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package OTRS::Repository;
2              
3             # ABSTRACT: parse OTRS repositories' otrs.xml files to search for add ons
4              
5 4     4   190521 use strict;
  4         37  
  4         102  
6 4     4   29 use warnings;
  4         8  
  4         87  
7              
8 4     4   1321 use Moo;
  4         32017  
  4         16  
9 4     4   4228 use List::Util qw(all);
  4         8  
  4         372  
10 4     4   23 use Scalar::Util qw(blessed);
  4         7  
  4         150  
11 4     4   1410 use Regexp::Common qw(URI);
  4         14764  
  4         17  
12              
13 4     4   72433 use OTRS::Repository::Source;
  0            
  0            
14              
15             our $VERSION = 0.09;
16              
17             our $ALLOWED_SCHEME = [ 'HTTP', 'file' ];
18              
19             has sources => ( is => 'ro', required => 1, isa => sub {
20             die "no valid URIs" unless
21             ref $_[0] eq 'ARRAY'
22             and
23             all { _check_uri( $_ ) } @{ $_[0] }
24             });
25              
26             has _objects => ( is => 'ro', isa => sub {
27             die "no valid objects" unless
28             ref $_[0] eq 'ARRAY'
29             and
30             all { blessed $_ and $_->isa( 'OTRS::Repository::Source' ) } @{ $_[0] }
31             });
32              
33             sub find {
34             my ($self, %params) = @_;
35              
36             my @found;
37             for my $source ( @{ $self->_objects || [] } ) {
38             my $found = $source->find( %params );
39             push @found, $found if $found;
40             }
41              
42             return @found;
43             }
44              
45             sub list {
46             my ($self, %params) = @_;
47              
48             my %found_packages;
49             my @detailed_list;
50             for my $source ( @{ $self->_objects || [] } ) {
51             my @found = $source->list( %params );
52             @found_packages{@found} = (1) x @found;
53             push @detailed_list, @found;
54             }
55              
56             if ( $params{details} ) {
57             return sort { $a->{name} cmp $b->{name} || $a->{version} cmp $b->{version} }@detailed_list;
58             }
59              
60             return sort keys %found_packages;
61             }
62              
63             sub BUILDARGS {
64             my ($class, @args) = @_;
65              
66             unshift @args, 'sources' if @args % 2;
67              
68             my %param = @args;
69              
70             for my $url ( @{ $param{sources} || [] } ) {
71             push @{ $param{_objects} }, OTRS::Repository::Source->new( url => $url );
72             }
73              
74             return \%param;
75             }
76              
77             sub _check_uri {
78             my @allowed_schemes = ref $ALLOWED_SCHEME ? @{ $ALLOWED_SCHEME } : $ALLOWED_SCHEME;
79              
80             my $matches;
81              
82             SCHEME:
83             for my $scheme ( @allowed_schemes ) {
84             my $regex = ( lc $scheme eq 'http' ) ?
85             $RE{URI}{HTTP}{-scheme => qr/https?/} :
86             $RE{URI}{$scheme};
87              
88             if ( $_[0] =~ m{\A$regex\z} ) {
89             $matches++;
90             last SCHEME;
91             }
92             }
93              
94             die "No valid URI" unless $matches;
95             return 1;
96             }
97              
98             1;
99              
100             __END__