PiedWeb /
CMS
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace PiedWeb\CMSBundle\Extension\UpdateNotification; |
||||
| 4 | |||||
| 5 | use DateInterval; |
||||
| 6 | use Doctrine\ORM\EntityManagerInterface; |
||||
| 7 | use PiedWeb\CMSBundle\Utils\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 PageUpdateMailNotifier |
||||
| 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 $pageClass; |
||||
| 26 | |||||
| 27 | public function __construct( |
||||
| 28 | string $pageClass, |
||||
| 29 | MailerInterface $mailer, |
||||
| 30 | string $emailFrom, |
||||
| 31 | ?string $emailTo, |
||||
| 32 | string $appName, |
||||
| 33 | string $rootDir, |
||||
| 34 | string $interval, //minIntervalBetweenTwoNotification |
||||
| 35 | EntityManagerInterface $entityManager, |
||||
| 36 | TranslatorInterface $translator |
||||
| 37 | ) { |
||||
| 38 | $this->mailer = $mailer; |
||||
| 39 | $this->emailTo = $emailTo; |
||||
| 40 | $this->emailFrom = $emailFrom; |
||||
| 41 | $this->interval = $interval; |
||||
| 42 | $this->appName = $appName; |
||||
| 43 | $this->rootDir = $rootDir; |
||||
| 44 | $this->em = $entityManager; |
||||
| 45 | $this->translator = $translator; |
||||
| 46 | $this->pageClass = $pageClass; |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | protected function getPageUpdatedSince($datetime) |
||||
| 50 | { |
||||
| 51 | $query = $this->em->createQuery( |
||||
| 52 | 'SELECT p FROM '.$this->pageClass.' p WHERE p.createdAt > :lastTime OR p.updatedAt > :lastTime' |
||||
| 53 | )->setParameter('lastTime', $datetime); |
||||
| 54 | |||||
| 55 | return $query->getResult(); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | public function postUpdate($page) |
||||
|
0 ignored issues
–
show
|
|||||
| 59 | { |
||||
| 60 | $this->send(); |
||||
| 61 | } |
||||
| 62 | |||||
| 63 | public function postPersist($page) |
||||
|
0 ignored issues
–
show
The parameter
$page is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 64 | { |
||||
| 65 | $this->send(); |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | public function send() |
||||
| 69 | { |
||||
| 70 | if (! $this->emailTo) { |
||||
| 71 | return; |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | $lastTime = new LastTime($this->rootDir.'/../var/lastPageUpdateNotification'); |
||||
| 75 | if (false === $lastTime->wasRunSince(new DateInterval($this->interval))) { |
||||
| 76 | return; |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | $pages = $this->getPageUpdatedSince($lastTime->get('15 minutes ago')); |
||||
| 80 | if (empty($pages)) { |
||||
| 81 | return; |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | $message = (new TemplatedEmail()) |
||||
| 85 | ->subject( |
||||
| 86 | $this->translator->trans('admin.page.update_notification.title', ['%appName%' => $this->appName]) |
||||
| 87 | ) |
||||
| 88 | ->from($this->emailFrom) |
||||
| 89 | ->to($this->emailTo) |
||||
| 90 | ->htmlTemplate('@pwcUpdateNotification/pageUpdateMailNotification.html.twig') |
||||
| 91 | ->context([ |
||||
| 92 | 'appName' => $this->appName, |
||||
| 93 | 'pages' => $pages, |
||||
| 94 | ]); |
||||
| 95 | |||||
| 96 | $lastTime->set(); |
||||
| 97 | $this->mailer->send($message); |
||||
| 98 | } |
||||
| 99 | } |
||||
| 100 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.