File Coverage

blib/lib/VLGal/File/Factory.pm
Criterion Covered Total %
statement 39 42 92.8
branch 9 18 50.0
condition 5 15 33.3
subroutine 10 10 100.0
pod 3 3 100.0
total 66 88 75.0


line stmt bran cond sub pod time code
1             package VLGal::File::Factory;
2              
3 1     1   19 use 5.006;
  1         2  
  1         29  
4 1     1   4 use strict;
  1         1  
  1         44  
5 1     1   5 use warnings;
  1         2  
  1         27  
6 1     1   6 use Error qw(:try);
  1         2  
  1         7  
7 1     1   1258 use File::MMagic;
  1         7592  
  1         31  
8 1     1   8 use File::Spec;
  1         1  
  1         422  
9              
10             # MMagic class variable
11             our $MMAGIC = undef;
12              
13             # Singleton variable
14             our $SINGLETON = undef;
15              
16             # Package version
17             our ($VERSION) = '$Revision: 0.01 $' =~ /\$Revision:\s+([^\s]+)/;
18              
19             =head1 NAME
20              
21             VLGal::File::Factory - Vincenzo's little gallery file factory
22              
23             =head1 SYNOPSIS
24              
25             TODO
26              
27             =head1 ABSTRACT
28              
29             Vincenzo's little gallery file factory
30              
31             =head1 DESCRIPTION
32              
33             C is a factory class to create C objects.
34              
35             =head1 CONSTRUCTOR
36              
37             =over
38              
39             =item new()
40              
41             Creates a new C object.
42              
43             =back
44              
45             =head1 METHODS
46              
47             =over
48              
49             =item create_file(OPT_HASH_REF)
50              
51             Creates a C object based on the type of the file -put together fith options C and C. C is a hash reference used to pass initialization options for the C object. On error an exception C is thrown.
52              
53             =item instance( [ CONSTR_OPT ] )
54              
55             Always returns the same C -singleton- object instance. The first time it is called, parameters C -if specified- are passed to the constructor.
56              
57             =back
58              
59             =head1 SEE ALSO
60              
61             L,
62             L,
63             L,
64             L,
65             L
66              
67             =head1 BUGS
68              
69             None known (yet.)
70              
71             =head1 HISTORY
72              
73             First development: September 2003
74             Last update: October 2003
75              
76             =head1 AUTHOR
77              
78             Vincenzo Zocca
79              
80             =head1 COPYRIGHT
81              
82             Copyright 2003 by Vincenzo Zocca
83              
84             =head1 LICENSE
85              
86             This file is part of the C module hierarchy for Perl by
87             Vincenzo Zocca.
88              
89             The VLGal module hierarchy is free software; you can redistribute it
90             and/or modify it under the terms of the GNU General Public License
91             as published by the Free Software Foundation; either version 2 of
92             the License, or (at your option) any later version.
93              
94             The VLGal module hierarchy is distributed in the hope that it will
95             be useful, but WITHOUT ANY WARRANTY; without even the implied
96             warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
97             See the GNU General Public License for more details.
98              
99             You should have received a copy of the GNU General Public License
100             along with the VLGal module hierarchy; if not, write to
101             the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
102             Boston, MA 02111-1307 USA
103              
104             =cut
105              
106             sub new {
107 1     1 1 3 my $class = shift;
108              
109 1         2 my $self = {};
110 1   33     7 bless( $self, ( ref($class) || $class ) );
111 1         3 return( $self->_initialize(@_) );
112             }
113              
114             sub _initialize {
115 1     1   3 my $self = shift;
116 1 50       4 my $opt = defined($_[0]) ? shift : {};
117              
118             # Check $opt
119 1 50       12 ref($opt) eq 'HASH' || throw Error::Simple("ERROR: VLGal::File::Factory::_initialize, first argument must be 'HASH' reference.");
120              
121             # Return $self
122 1         4 return($self);
123             }
124              
125             sub create_file {
126 1     1 1 3 my $self = shift;
127 1 50       3 my $opt = defined($_[0]) ? shift : {};
128              
129             # Make file name and file type from options
130 1         17 my $fs_name = File::Spec->catfile( $opt->{dirname}, $opt->{basename} );
131              
132             # Do directories first
133 1 50       14 ( -d $fs_name ) && return( VLGal::Directory->new_from_fs($opt) );
134              
135             # Make $MMAGIC
136 1 50       10 $MMAGIC = File::MMagic->new() if( !defined($MMAGIC) );
137              
138             # Switch for supported types
139 1         486 my $mime_type = $MMAGIC->checktype_filename($fs_name);
140 1 50 33     14263 if ( $mime_type eq 'image/gif' ||
      33        
141             $mime_type eq 'image/jpeg' ||
142             $mime_type eq 'image/png'
143             ) {
144 1         759 require VLGal::File::MMagic;
145 0         0 return( VLGal::File::MMagic->new($opt) );
146             }
147              
148             # Return undef if nothing can be created
149 0         0 return(undef);
150             }
151              
152             sub instance {
153             # Allow calls like:
154             # - VLGal::File::Factory::instance()
155             # - VLGal::File::Factory->instance()
156             # - $variable->instance()
157 1 50 33 1 1 11 if ( ref($_[0]) && &UNIVERSAL::isa( $_[0], 'VLGal::File::Factory' ) ) {
    50 33        
158 0         0 shift;
159             }
160             elsif ( ! ref($_[0]) && $_[0] eq 'VLGal::File::Factory' ) {
161 1         3 shift;
162             }
163              
164             # If $SINGLETON is defined return it
165 1 50       3 defined($SINGLETON) && return($SINGLETON);
166              
167             # Create the object and set $SINGLETON
168 1         6 $SINGLETON = VLGal::File::Factory->new(@_);
169              
170             # Return $SINGLETON
171 1         5 return($SINGLETON);
172             }
173              
174             1;