Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

MailFetcher.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use App\Presentation\PortfolioPresenter;
  5. use App\Service\TelegramNotifier;
  6. use PhpImap\Exceptions\ConnectionException;
  7. use PhpImap\IncomingMail;
  8. use Psr\Log\LoggerInterface;
  9. use SecIT\ImapBundle\Connection\ConnectionInterface;
  10. use Symfony\Component\DependencyInjection\Attribute\Target;
  11. readonly class MailFetcher
  12. {
  13. public function __construct(
  14. #[Target('finfollowConnection')]
  15. private ConnectionInterface $connection,
  16. private XmlParser $xmlParser,
  17. private PortfolioManager $portfolioManager,
  18. private LoggerInterface $logger,
  19. private TelegramNotifier $telegramNotifier,
  20. private PortfolioPresenter $portfolioPresenter,
  21. private AttachmentProcessor $attachmentProcessor,
  22. private string $validEmailSubject,
  23. private string $validEmailSender,
  24. )
  25. {
  26. }
  27. public function fetchNewEmails(): void
  28. {
  29. $mailbox = $this->connection->getMailbox();
  30. try {
  31. $mailsIds = $mailbox->searchMailbox('ALL');
  32. } catch (ConnectionException $ex) {
  33. $this->logger->error("IMAP connection failed: " . implode(",", $ex->getErrors('all')));
  34. throw new \RuntimeException('IMAP connection failed', 0, $ex);
  35. }
  36. if (!$mailsIds) {
  37. $this->logger->info('Mailbox is empty');
  38. return;
  39. }
  40. foreach ($mailsIds as $mailId) {
  41. $mail = $mailbox->getMail($mailId);
  42. $this->logger->debug(print_r([
  43. 'date' => $mail->date,
  44. 'message_id' => $mail->messageId,
  45. 'from' => $mail->fromAddress,
  46. 'subject' => $mail->subject,
  47. 'hasAttachments' => $mail->hasAttachments(),
  48. ], true));
  49. if ($this->canProcessMail($mail)) {
  50. $this->processMail($mail);
  51. $mailbox->deleteMail($mailId);
  52. $this->logger->debug("Deleted email with id=" . $mailId);
  53. } else {
  54. $this->logger->debug("Skipped mail with id=" . $mailId);
  55. }
  56. }
  57. }
  58. private function canProcessMail(IncomingMail $mail): bool
  59. {
  60. if (!$mail->hasAttachments()) {
  61. return false;
  62. }
  63. if (mb_stripos($mail->subject, $this->validEmailSubject) === false) {
  64. return false;
  65. }
  66. if (mb_stripos($mail->fromAddress, $this->validEmailSender) === false) {
  67. return false;
  68. }
  69. return true;
  70. }
  71. private function processMail(IncomingMail $mail): void
  72. {
  73. foreach ($mail->getAttachments() as $attachment) {
  74. $xmlString = $this->attachmentProcessor->extractXmlFromAttachment($attachment);
  75. if ($xmlString === null) {
  76. continue;
  77. }
  78. $xml = simplexml_load_string($xmlString);
  79. if ($xml === false) {
  80. $this->logger->error('Invalid XML from attachment');
  81. continue;
  82. }
  83. $parsedPortfolio = $this->xmlParser->processXml($xml);
  84. if ($this->portfolioManager->updatePortfolio($parsedPortfolio, $xmlString)) {
  85. $pngFilename = tempnam(sys_get_temp_dir(), 'finfollow-portfolio-');
  86. $this->portfolioPresenter->toImage($parsedPortfolio, $pngFilename);
  87. $this->telegramNotifier->notify('', $pngFilename);
  88. }
  89. }
  90. }
  91. }