|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\ConversationBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use PiedWeb\CMSBundle\Service\LastTime; |
|
8
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
|
9
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Move it to a plugin (todo). |
|
14
|
|
|
*/ |
|
15
|
|
|
class NewMessageMailNotifier |
|
16
|
|
|
{ |
|
17
|
|
|
private $mailer; |
|
18
|
|
|
private $emailTo; |
|
19
|
|
|
private $emailFrom; |
|
20
|
|
|
private $appName; |
|
21
|
|
|
private $rootDir; |
|
22
|
|
|
private $interval; |
|
23
|
|
|
private $em; |
|
24
|
|
|
private $translator; |
|
25
|
|
|
private $message; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Undocumented function. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $message Entity |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct( |
|
33
|
|
|
string $message, |
|
34
|
|
|
MailerInterface $mailer, |
|
35
|
|
|
string $emailFrom, |
|
36
|
|
|
?string $emailTo, |
|
37
|
|
|
string $appName, |
|
38
|
|
|
string $rootDir, |
|
39
|
|
|
string $interval, //minIntervalBetweenTwoNotification |
|
40
|
|
|
EntityManagerInterface $entityManager, |
|
41
|
|
|
TranslatorInterface $translator |
|
42
|
|
|
) { |
|
43
|
|
|
$this->mailer = $mailer; |
|
44
|
|
|
$this->emailTo = $emailTo; |
|
45
|
|
|
$this->emailFrom = $emailFrom; |
|
46
|
|
|
$this->interval = $interval; |
|
47
|
|
|
$this->appName = $appName; |
|
48
|
|
|
$this->rootDir = $rootDir; |
|
49
|
|
|
$this->em = $entityManager; |
|
50
|
|
|
$this->translator = $translator; |
|
51
|
|
|
$this->message = $message; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getMessagesPostedSince($datetime) |
|
55
|
|
|
{ |
|
56
|
|
|
$query = $this->em->createQuery( |
|
57
|
|
|
'SELECT m FROM '.$this->message.' m WHERE m.createdAt > :lastNotificationTime' |
|
58
|
|
|
)->setParameter('lastNotificationTime', $datetime); |
|
59
|
|
|
|
|
60
|
|
|
return $query->getResult(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function send() |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->emailTo) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$lastTime = new LastTime($this->rootDir.'/../var/lastNewMessageNotification'); |
|
70
|
|
|
if (false === $lastTime->wasRunSince(new DateInterval($this->interval))) { |
|
71
|
|
|
return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$messages = $this->getMessagesPostedSince($lastTime->get('15 minutes ago')); |
|
75
|
|
|
if (empty($messages)) { |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$message = (new TemplatedEmail()) |
|
80
|
|
|
->subject( |
|
81
|
|
|
$this->translator->trans( |
|
82
|
|
|
'admin.conversation.notification.title.'.(count($messages) > 1 ? 'plural' : 'singular'), |
|
83
|
|
|
['%appName%' => $this->appName] |
|
84
|
|
|
) |
|
85
|
|
|
) |
|
86
|
|
|
->from($this->emailFrom) |
|
87
|
|
|
->to($this->emailTo) |
|
88
|
|
|
->htmlTemplate('@PiedWebConversation/notification.html.twig') |
|
89
|
|
|
->context([ |
|
90
|
|
|
'appName' => $this->appName, |
|
91
|
|
|
'messages' => $messages, |
|
92
|
|
|
]); |
|
93
|
|
|
|
|
94
|
|
|
$lastTime->set(); |
|
95
|
|
|
$this->mailer->send($message); |
|
96
|
|
|
|
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|