1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package EBloodBank |
4
|
|
|
* @since 1.6 |
5
|
|
|
*/ |
6
|
|
|
namespace EBloodBank\Listeners; |
7
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
use EBloodBank\Options; |
10
|
|
|
use EBloodBank\Models\User; |
11
|
|
|
use EBloodBank\Models\Donor; |
12
|
|
|
use EBloodBank\Traits\MailerTrait; |
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @since 1.6 |
18
|
|
|
*/ |
19
|
|
|
class EmailNotificationListener |
20
|
|
|
{ |
21
|
|
|
use MailerTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param ContainerInterface $container |
25
|
|
|
* @return EmailNotificationListener |
26
|
|
|
* @since 1.6 |
27
|
|
|
* @static |
28
|
|
|
*/ |
29
|
|
|
public static function factory(ContainerInterface $container) |
30
|
|
|
{ |
31
|
|
|
$listener = new static(); |
32
|
|
|
$listener->setMailer($container->get('mailer')); |
33
|
|
|
|
34
|
|
|
return $listener; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param GenericEvent $event |
39
|
|
|
* @return void |
40
|
|
|
* @since 1.6 |
41
|
|
|
*/ |
42
|
|
|
public function __invoke(GenericEvent $event, $eventName) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
$subject = $event->getSubject(); |
46
|
|
|
|
47
|
|
|
if ('donor.created' === $eventName && $subject instanceof Donor) { |
48
|
|
|
$message = $this->getMailer()->createMessage(); |
49
|
|
|
|
50
|
|
|
$message->setSubject(sprintf(__('[%s] New Donor'), Options::getOption('site_name'))); |
51
|
|
|
$message->setFrom(Options::getOption('site_email')); |
52
|
|
|
$message->setTo(Options::getOption('site_email')); |
53
|
|
|
|
54
|
|
|
$messageBody = sprintf(__('New donor addition on %s:'), Options::getOption('site_name')) . "\r\n\r\n"; |
55
|
|
|
$messageBody .= sprintf(__('Name: %s'), $subject->get('name')) . "\r\n"; |
56
|
|
|
$messageBody .= sprintf(__('Gender: %s'), $subject->getGenderTitle()) . "\r\n"; |
57
|
|
|
$messageBody .= sprintf(__('Blood Group: %s'), $subject->get('blood_group')) . "\r\n"; |
58
|
|
|
$messageBody .= sprintf(__('City\District: %1$s\%2$s'), $subject->get('district')->get('city')->get('name'), $subject->get('district')->get('name')); |
59
|
|
|
|
60
|
|
|
$message->setBody($messageBody, 'text/plain'); |
61
|
|
|
|
62
|
|
|
$this->getMailer()->send($message); |
63
|
|
|
} elseif ('user.created' === $eventName && $subject instanceof User) { |
64
|
|
|
$message = $this->getMailer()->createMessage(); |
65
|
|
|
|
66
|
|
|
$message->setSubject(sprintf(__('[%s] New User Registration'), Options::getOption('site_name'))); |
67
|
|
|
$message->setFrom(Options::getOption('site_email')); |
68
|
|
|
$message->setTo(Options::getOption('site_email')); |
69
|
|
|
|
70
|
|
|
$messageBody = sprintf(__('New user registration on %s:'), Options::getOption('site_name')) . "\r\n\r\n"; |
71
|
|
|
$messageBody .= sprintf(__('Username: %s'), $subject->get('name')) . "\r\n"; |
72
|
|
|
$messageBody .= sprintf(__('E-mail: %s'), $subject->get('email')) . "\r\n"; |
73
|
|
|
|
74
|
|
|
$message->setBody($messageBody, 'text/plain'); |
75
|
|
|
|
76
|
|
|
$this->getMailer()->send($message); |
77
|
|
|
} |
78
|
|
|
} catch (Exception $ex) { |
79
|
|
|
// TODO: Log the exception. |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|