|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\SwiftMailerCallable; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
use Psr\Log\NullLogger; |
|
6
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
7
|
|
|
|
|
8
|
|
|
class SwiftMailerCallable implements LoggerAwareInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Swift_Mailer |
|
13
|
|
|
*/ |
|
14
|
|
|
public $mailer; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Callable |
|
18
|
|
|
*/ |
|
19
|
|
|
public $message_factory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var LoggerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
public $logger; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Swift_Mailer $mailer |
|
30
|
|
|
* @param Callable $message_factory Factory returning new Swift_Message instance |
|
31
|
|
|
*/ |
|
32
|
36 |
|
public function __construct( \Swift_Mailer $mailer, Callable $message_factory, LoggerInterface $logger = null ) |
|
33
|
|
|
{ |
|
34
|
36 |
|
$this->mailer = $mailer; |
|
35
|
36 |
|
$this->message_factory = $message_factory; |
|
36
|
36 |
|
$this->setLogger($logger ?: new NullLogger); |
|
37
|
36 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $subject Mail Subject |
|
41
|
|
|
* @param string $text Mail Body |
|
42
|
|
|
* @param string|array $to Recipients |
|
43
|
|
|
* |
|
44
|
|
|
* @return int |
|
45
|
|
|
* |
|
46
|
|
|
* @throws RuntimeException if factory did not return instance of Swift_Message |
|
47
|
|
|
*/ |
|
48
|
35 |
|
public function __invoke($subject, $text, $to = null) |
|
49
|
|
|
{ |
|
50
|
35 |
|
$message_factory = $this->message_factory; |
|
51
|
35 |
|
$message = $message_factory(); |
|
52
|
|
|
|
|
53
|
35 |
|
if (!$message instanceOf \Swift_Message) { |
|
54
|
5 |
|
throw new \RuntimeException( 'Factory did not return Swift_Message instance.'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
30 |
|
$subject = trim(implode(" ", [ |
|
58
|
30 |
|
$message->getSubject(), |
|
59
|
24 |
|
$subject |
|
60
|
6 |
|
])); |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
30 |
|
$message->setSubject( $subject ); |
|
64
|
|
|
|
|
65
|
30 |
|
$message->setBody( $text ); |
|
66
|
|
|
|
|
67
|
30 |
|
if ($to) { |
|
68
|
10 |
|
$to = is_array($to) ? $to : [ $to ]; |
|
69
|
10 |
|
$message->setTo( $to ); |
|
70
|
2 |
|
} else { |
|
71
|
20 |
|
$to = array(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
30 |
|
if ($result = $this->mailer->send( $message )): |
|
76
|
25 |
|
$this->logger->info("Successfully sent mail", ['to' => implode(", ", $to) ]); |
|
77
|
5 |
|
else: |
|
78
|
5 |
|
$this->logger->error("Failed sending mail", ['to' => implode(", ", $to) ]); |
|
79
|
|
|
endif; |
|
80
|
|
|
|
|
81
|
30 |
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets a logger instance on the object. |
|
86
|
|
|
* |
|
87
|
|
|
* @param LoggerInterface $logger |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
36 |
|
public function setLogger(LoggerInterface $logger) |
|
92
|
|
|
{ |
|
93
|
36 |
|
$this->logger = $logger; |
|
94
|
36 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|