| 1 | package Net::AS2::PSGI::File; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use autodie qw(:file :filesys); | ||
| 6 | |||
| 7 | our $VERSION = '1.0'; # VERSION | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Net::AS2::PSGI::File - Base class for managing files in the AS2 PSGI application | ||
| 12 | |||
| 13 | =head1 CONSTRUCTOR | ||
| 14 | |||
| 15 | =over 4 | ||
| 16 | |||
| 17 | =item $file = $class->new($message_id, $logger) | ||
| 18 | |||
| 19 | Returns an object for handling files used with the AS2 PSGI | ||
| 20 | application. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($class, $message_id, $logger) = @_; | ||
| 26 | |||
| 27 | my $self = bless { logger => $logger }, $class; | ||
| 28 | |||
| 29 | $self->message_id($message_id); | ||
| 30 | |||
| 31 | return $self; | ||
| 32 | } | ||
| 33 | |||
| 34 | =back | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item $message_id = $self->message_id( $id ) | ||
| 41 | |||
| 42 | Set, if given C<$id>, or get the current message_id. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub message_id { | ||
| 47 | my ($self, $message_id) = @_; | ||
| 48 | |||
| 49 | if ($message_id) { | ||
| 50 | $self->{message_id} = $message_id; | ||
| 51 | |||
| 52 | $message_id =~ s{[^-@.a-zA-Z0-9]}{_}g; | ||
| 53 | $self->{message_filename} = $message_id; | ||
| 54 | } | ||
| 55 | |||
| 56 | return $self->{message_id}; | ||
| 57 | } | ||
| 58 | |||
| 59 | =item $file = $self->file( $dir, $ext ) | ||
| 60 | |||
| 61 | Return suitable file name in the given directory, C<dir>, using the | ||
| 62 | associated message id and optional extension, C<ext>. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub file { | ||
| 67 | my ($self, $dir, $ext) = @_; | ||
| 68 | |||
| 69 | $ext //= ''; | ||
| 70 | |||
| 71 | return sprintf('%s/%s%s', $dir, $self->{message_filename}, $ext); | ||
| 72 | } | ||
| 73 | |||
| 74 | =item $self->logger( $level, $text ) | ||
| 75 | |||
| 76 | Outputs a message to the PSGI logger at the given level, if a PSGI | ||
| 77 | logger has been defined. | ||
| 78 | |||
| 79 | The full message output format is set to the logger with the given | ||
| 80 | C<level>: | ||
| 81 | |||
| 82 | <message_id> : text | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub logger { | ||
| 87 | my ($self, $level, $text) = @_; | ||
| 88 | |||
| 89 | my $logger = $self->{logger} or return; | ||
| 90 | |||
| 91 | my $message_id = $self->message_id; | ||
| 92 | |||
| 93 | $logger->({ level => $level, message => sprintf('<%s> : %s', $message_id, $text) }); | ||
| 94 | |||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | =item $self->write( $file, $content ) | ||
| 99 | |||
| 100 | Writes C<$content> to C<$file>. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub write { ## no critic (ProhibitBuiltinHomonyms) | ||
| 105 | my ($self, $file, $content) = @_; | ||
| 106 | |||
| 107 | $content //= ''; | ||
| 108 | |||
| 109 | open my $fh, '>', $file; | ||
| 110 | print $fh $content; | ||
| 111 | close $fh; | ||
| 112 | |||
| 113 | return; | ||
| 114 | } | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | 1; |