From 0d006701a359473d0d772b28b10e280ffdfac726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=BDold=C3=A1k?= Date: Mon, 4 Sep 2023 12:57:26 +0000 Subject: [PATCH] Refactored payment confirmation commands remp/crm#2878 - Removed redundant mail downloader classes (`CsobMailDownloader`, `SkCsobMailDownloader`, `TatraBankaMailDownloader`, `TatraBankaStatementMailDownloader`, `SlspMailDownloader`, `VubMailDownloader`) and moved logic directly to commands - Abstracted direct dependency on `Tomaj\ImapMailDownloader\Downloader` to `ImapMailDownloader` - Replaced direct dependency on `Tomaj\ImapMailDownloader\Downloader` with `MailDownloaderInterface` in confirmation commands - Added option to replace default mail downloader `ImapMailDownloader` (downloader must implement: `MailDownloaderInterface`) --- src/Commands/SlspMailConfirmationCommand.php | 110 +++++++++++++++--- .../MailDownloader/SlspMailDownloader.php | 57 --------- .../SlspNotificationMailDownloader.php | 100 ---------------- src/config/config.neon | 4 +- 4 files changed, 92 insertions(+), 179 deletions(-) delete mode 100644 src/Models/MailDownloader/SlspMailDownloader.php delete mode 100644 src/Models/MailDownloader/SlspNotificationMailDownloader.php diff --git a/src/Commands/SlspMailConfirmationCommand.php b/src/Commands/SlspMailConfirmationCommand.php index a13fd4f..f1ad682 100644 --- a/src/Commands/SlspMailConfirmationCommand.php +++ b/src/Commands/SlspMailConfirmationCommand.php @@ -2,30 +2,32 @@ namespace Crm\SlspSporopayModule\Commands; +use Crm\ApplicationModule\Config\ApplicationConfig; use Crm\PaymentsModule\MailConfirmation\MailProcessor; -use Crm\SlspSporopayModule\MailConfirmation\SlspMailDownloader; -use Crm\SlspSporopayModule\MailConfirmation\SlspNotificationMailDownloader; +use Crm\PaymentsModule\Models\MailDownloader\EmailInterface; +use Crm\PaymentsModule\Models\MailDownloader\MailDownloaderInterface; +use Crm\SlspSporopayModule\MailParser\SlspMailParser; +use Crm\SlspSporopayModule\MailParser\SlspNotificationMailParser; +use Nette\Utils\FileSystem; +use Nette\Utils\Random; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Tomaj\ImapMailDownloader\MailCriteria; +use Tracy\Debugger; +use Tracy\ILogger; class SlspMailConfirmationCommand extends Command { - private $notificationMailDownloader; - - private $mailDownloader; - - private $mailProcessor; + private OutputInterface $output; public function __construct( - SlspNotificationMailDownloader $notificationMailDownloader, - SlspMailDownloader $mailDownloader, - MailProcessor $mailProcessor + private string $confirmationTmpDir, + private MailDownloaderInterface $mailDownloader, + private MailProcessor $mailProcessor, + private ApplicationConfig $applicationConfig, ) { parent::__construct(); - $this->notificationMailDownloader = $notificationMailDownloader; - $this->mailDownloader = $mailDownloader; - $this->mailProcessor = $mailProcessor; } protected function configure() @@ -36,19 +38,89 @@ protected function configure() protected function execute(InputInterface $input, OutputInterface $output) { - $this->mailDownloader->download(function ($mailContent) use ($output) { - return $this->markMailProcessed($mailContent, $output); + $this->output = $output; + + $connectionOptions = [ + 'imapHost' => $this->applicationConfig->get('slsp_confirmation_host'), + 'imapPort' => $this->applicationConfig->get('slsp_confirmation_port'), + 'username' => $this->applicationConfig->get('slsp_confirmation_username'), + 'password' => $this->applicationConfig->get('slsp_confirmation_password'), + 'processedFolder' => $this->applicationConfig->get('slsp_confirmation_processed_folder'), + ]; + + $criteria = new MailCriteria(); + $criteria->setFrom('vypis@slsp.sk'); + $criteria->setSubject('Informacia o realizacii platby'); + $criteria->setUnseen(true); + $connectionOptions['criteria'] = $criteria; + + $this->mailDownloader->download($connectionOptions, function (EmailInterface $email) { + $slspMailParser = new SlspMailParser(); + + $mailContent = $slspMailParser->parse(quoted_printable_decode($email->getBody())); + if ($mailContent !== null) { + $this->mailProcessor->processMail($mailContent, $this->output); + } }); - $this->notificationMailDownloader->download(function ($mailContent) use ($output) { - return $this->markMailProcessed($mailContent, $output); + $connectionOptions = [ + 'imapHost' => $this->applicationConfig->get('slsp_notification_confirmation_host'), + 'imapPort' => $this->applicationConfig->get('slsp_notification_confirmation_port'), + 'username' => $this->applicationConfig->get('slsp_notification_confirmation_username'), + 'password' => $this->applicationConfig->get('slsp_notification_confirmation_password'), + 'processedFolder' => $this->applicationConfig->get('slsp_notification_confirmation_processed_folder'), + ]; + + $criteria = new MailCriteria(); + $criteria->setFrom('notifikacie@slsp.sk'); + $criteria->setSubject('Notifikacia'); + $criteria->setUnseen(true); + + $options = array_merge($connectionOptions, ['criteria' => $criteria]); + $this->mailDownloader->download($options, function (EmailInterface $email) { + $parser = new SlspNotificationMailParser(); + + $body = $this->getAttachmentBody($email); + $mailContent = $parser->parse(quoted_printable_decode($body)); + if ($mailContent !== null && $body) { + $this->mailProcessor->processMail($mailContent, $this->output); + } }); return Command::SUCCESS; } - private function markMailProcessed($mailContent, $output) + private function validateAttachment(EmailInterface $email): bool { - return !$this->mailProcessor->processMail($mailContent, $output); + $attachments = $email->getAttachments(); + if (empty($attachments)) { + Debugger::log( + 'missing slsp notification mail attachment for payment sent on: ' . $email->getDate(), + ILogger::WARNING + ); + return false; + } + return true; + } + + private function getAttachmentBody(EmailInterface $email): bool|string + { + if (!$this->validateAttachment($email)) { + return false; + } + + $filePath = $this->confirmationTmpDir . DIRECTORY_SEPARATOR . 'payments-mail-parser/' . Random::generate() . '.zip'; + + FileSystem::write($filePath, array_values($email->getAttachments())[0]['attachment']); + + $zip = new \ZipArchive(); + $zip->open($filePath); + $contents = $zip->getFromIndex(0); + + $contents = iconv("ISO-8859-1", "UTF-8", $contents); + + FileSystem::delete($filePath); + + return $contents; } } diff --git a/src/Models/MailDownloader/SlspMailDownloader.php b/src/Models/MailDownloader/SlspMailDownloader.php deleted file mode 100644 index 1de27d4..0000000 --- a/src/Models/MailDownloader/SlspMailDownloader.php +++ /dev/null @@ -1,57 +0,0 @@ -imapHost = $config->get('slsp_confirmation_host'); - $this->imapPort = $config->get('slsp_confirmation_port'); - $this->username = $config->get('slsp_confirmation_username'); - $this->password = $config->get('slsp_confirmation_password'); - $this->processedFolder = $config->get('slsp_confirmation_processed_folder'); - } - - public function download($callback) - { - $downloader = new Downloader( - $this->imapHost, - $this->imapPort, - $this->username, - $this->password, - $this->processedFolder - ); - - $criteria = new MailCriteria(); - $criteria->setFrom('vypis@slsp.sk'); - $criteria->setSubject('Informacia o realizacii platby'); - $criteria->setUnseen(true); - $downloader->fetch($criteria, function (Email $email) use ($callback) { - $slspMailParser = new SlspMailParser(); - - $mailContent = $slspMailParser->parse(quoted_printable_decode($email->getBody())); - if (!empty($mailContent)) { - return $callback($mailContent); - } - - return false; - }); - } -} diff --git a/src/Models/MailDownloader/SlspNotificationMailDownloader.php b/src/Models/MailDownloader/SlspNotificationMailDownloader.php deleted file mode 100644 index feca089..0000000 --- a/src/Models/MailDownloader/SlspNotificationMailDownloader.php +++ /dev/null @@ -1,100 +0,0 @@ -tempDir = $tempDir; - - $this->imapHost = $config->get('slsp_notification_confirmation_host'); - $this->imapPort = $config->get('slsp_notification_confirmation_port'); - $this->username = $config->get('slsp_notification_confirmation_username'); - $this->password = $config->get('slsp_notification_confirmation_password'); - $this->processedFolder = $config->get('slsp_notification_confirmation_processed_folder'); - } - - public function download($callback) - { - $downloader = new Downloader( - $this->imapHost, - $this->imapPort, - $this->username, - $this->password, - $this->processedFolder - ); - - $criteria = new MailCriteria(); - $criteria->setFrom('notifikacie@slsp.sk'); - $criteria->setSubject('Notifikacia'); - $criteria->setUnseen(true); - $downloader->fetch($criteria, function (Email $email) use ($callback) { - $parser = new SlspNotificationMailParser(); - - $body = $this->getAttachmentBody($email); - $mailContent = $parser->parse(quoted_printable_decode($body)); - if (!empty($mailContent) && $body) { - return $callback($mailContent); - } - - return false; - }); - } - - public function validateAttachment(Email $email) - { - $attachments = $email->getAttachments(); - if (empty($attachments)) { - Debugger::log( - 'missing slsp notification mail attachment for payment sent on: ' . $email->getDate(), - ILogger::WARNING - ); - return false; - } - return true; - } - - public function getAttachmentBody(Email $email) - { - if (!$this->validateAttachment($email)) { - return false; - } - - $filePath = $this->tempDir . DIRECTORY_SEPARATOR . 'payments-mail-parser/' . Random::generate() . '.zip'; - - FileSystem::write($filePath, array_values($email->getAttachments())[0]['attachment']); - - $zip = new \ZipArchive(); - $zip->open($filePath); - $contents = $zip->getFromIndex(0); - - $contents = iconv("ISO-8859-1", "UTF-8", $contents); - - FileSystem::delete($filePath); - - return $contents; - } -} diff --git a/src/config/config.neon b/src/config/config.neon index f5f2d63..c0662f2 100644 --- a/src/config/config.neon +++ b/src/config/config.neon @@ -6,9 +6,7 @@ services: - Crm\SlspSporopayModule\Seeders\ConfigsSeeder - Crm\SlspSporopayModule\Seeders\PaymentGatewaysSeeder - Crm\SlspSporopayModule\Gateways\SlspSporopay - - Crm\SlspSporopayModule\MailConfirmation\SlspMailDownloader - - Crm\SlspSporopayModule\MailConfirmation\SlspNotificationMailDownloader(%tempDir%) - - Crm\SlspSporopayModule\Commands\SlspMailConfirmationCommand + - Crm\SlspSporopayModule\Commands\SlspMailConfirmationCommand(%tempDir%) gatewayFactory: setup: