File Coverage

blib/lib/Email/Sender/Server/Message.pm
Criterion Covered Total %
statement 15 70 21.4
branch 0 72 0.0
condition 0 44 0.0
subroutine 5 9 55.5
pod 0 4 0.0
total 20 199 10.0


line stmt bran cond sub pod time code
1             # ABSTRACT: Eventual Email Message Object
2              
3             package Email::Sender::Server::Message;
4              
5 1     1   5 use Moo;
  1         2  
  1         9  
6 1     1   398 use utf8;
  1         2  
  1         7  
7              
8             with 'Email::Sender::Server::Base';
9              
10 1     1   49 use Class::Date;
  1         2  
  1         8  
11 1     1   1224 use Emailesque;
  1         328831  
  1         100  
12 1     1   14 use Hash::Merge qw(merge);
  1         2  
  1         1793  
13              
14             our $VERSION = '1.000001'; # VERSION
15              
16              
17             my @attributes = qw(
18             to
19             from
20             subject
21             cc
22             bcc
23             reply_to
24             headers
25             attachments
26             message
27             type
28             tags
29             transport
30             result
31             config
32             );
33              
34             has \@attributes => (
35             is => 'rw'
36             );
37              
38             sub BUILDARGS {
39 0     0 0   my ($class, @args) = @_;
40              
41 0           my %args = @args % 2 == 1 ?
42             'HASH' eq ref $args[0] ?
43 0 0         %{$args[0]} : () : @args;
    0          
44              
45 0 0 0       if ($args{message} && ref $args{message}) {
46 0 0         if (exists $args{message}{message}) {
47 0           for (@attributes) {
48 0 0         next if $_ eq 'message';
49 0 0         $args{$_} = $args{message}{$_}
50             if $args{message}{$_};
51             }
52 0           $args{message} = $args{message}{message};
53 0           for (keys %args) {
54 0 0         delete $args{$_} unless $class->can($_);
55             }
56             }
57             }
58              
59 0           return \%args;
60             }
61              
62             sub BUILD {
63 0     0 0   my ($self) = @_;
64              
65             # set defaults from config file
66              
67 0           my $config = $self->filepath('config');
68              
69 0 0         if (-e $config) {
70 0           my $data = do $config;
71              
72 0 0         return $self unless "HASH" eq ref $data ;
73              
74 0 0 0       $self->config($data)
75             if $data && ! $self->config;
76              
77 0 0 0       $self->to($data->{message}->{to})
78             if $data->{message}->{to} && ! $self->to;
79              
80 0 0 0       $self->from($data->{message}->{from})
81             if $data->{message}->{from} && ! $self->from;
82              
83 0 0 0       $self->subject($data->{message}->{subject})
84             if $data->{message}->{subject} && ! $self->subject;
85              
86 0 0 0       $self->message($data->{message}->{message})
87             if $data->{message}->{message} && ! $self->message;
88              
89 0 0 0       $self->type($data->{message}->{type})
90             if $data->{message}->{type} && ! $self->type;
91              
92 0 0 0       $self->cc($data->{message}->{cc})
93             if $data->{message}->{cc} && ! $self->cc;
94              
95 0 0 0       $self->bcc($data->{message}->{bcc})
96             if $data->{message}->{bcc} && ! $self->bcc;
97              
98 0 0 0       $self->reply_to($data->{message}->{reply_to})
99             if $data->{message}->{reply_to} && ! $self->reply_to;
100              
101 0 0 0       $self->transport($data->{transport})
102             if $data->{transport} && ! $self->transport;
103              
104 0 0 0       $self->headers($data->{headers})
105             if $data->{headers} && ! $self->headers;
106              
107 0 0 0       $self->attachments($data->{attachments})
108             if $data->{attachments} && ! $self->attachments;
109              
110 0 0 0       $self->tags($data->{tags})
111             if $data->{tags} && ! $self->tags;
112             }
113              
114 0           return $self;
115             }
116              
117             sub to_hash {
118 0     0 0   my ($self) = @_;
119              
120 0           my $mail = {
121             message => {
122             to => $self->to,
123             from => $self->from,
124             subject => $self->subject
125             }
126             };
127              
128 0 0         $mail->{message}->{cc} = $self->cc
129             if $self->cc;
130              
131 0 0         $mail->{message}->{bcc} = $self->bcc
132             if $self->bcc;
133              
134 0 0         $mail->{message}->{reply_to} = $self->reply_to
135             if $self->reply_to;
136              
137 0 0         $mail->{message}->{message} = $self->message
138             if $self->message;
139              
140 0 0         $mail->{message}->{type} = $self->type
141             if $self->type;
142              
143 0 0         $mail->{transport} = $self->transport
144             if $self->transport;
145              
146 0           $mail->{created} = Class::Date::now->string;
147              
148 0 0         $mail->{headers} = $self->headers
149             if $self->headers;
150              
151 0 0         $mail->{attachments} = $self->attachments
152             if $self->attachments;
153              
154 0 0         $mail->{tags} = $self->tags
155             if $self->tags;
156              
157 0           return $mail;
158             };
159              
160             sub send {
161 0     0 0   my ($self) = @_;
162 0 0         my $mail = $self->to_hash or return;
163 0   0       my $conf = $self->config || { message => {} };
164              
165 0           $mail = {
166 0 0         %{$mail->{message}},
    0          
167             $mail->{headers} ? (headers => $mail->{headers}) : (),
168             $mail->{attachments} ? (attachments => $mail->{attachments}) : ()
169             };
170              
171 0 0         $mail = merge $mail, $conf->{message} if keys %$conf;
172              
173 0           my $email = Emailesque->new($mail);
174              
175 0           my $result = $email->send(
176 0 0         {} => $mail->{transport} ? %{$mail->{transport}} : ()
177             );
178              
179 0           $self->result($result);
180              
181 0           return $result;
182             };
183              
184             1;
185              
186             __END__