From 65d7affcb53619e9ce406d8d2e75d0a4b7206e68 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 20 Sep 2019 16:15:49 +0400 Subject: [PATCH] export mail : make it event oriented to facilitate worker treatement --- .../Controller/Prod/ExportController.php | 47 +++++--------- .../Phrasea/Core/Event/ExportMailEvent.php | 53 +++++++++++++++ .../Event/Subscriber/ExportSubscriber.php | 65 ++++++++++++++++++- lib/Alchemy/Phrasea/Core/PhraseaEvents.php | 3 +- .../Phrasea/Controller/Prod/ExportTest.php | 3 +- 5 files changed, 137 insertions(+), 34 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Core/Event/ExportMailEvent.php diff --git a/lib/Alchemy/Phrasea/Controller/Prod/ExportController.php b/lib/Alchemy/Phrasea/Controller/Prod/ExportController.php index df6f2a9f19..f4692c99ac 100644 --- a/lib/Alchemy/Phrasea/Controller/Prod/ExportController.php +++ b/lib/Alchemy/Phrasea/Controller/Prod/ExportController.php @@ -15,6 +15,7 @@ use Alchemy\Phrasea\Application\Helper\FilesystemAware; use Alchemy\Phrasea\Application\Helper\NotifierAware; use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Core\Event\ExportFailureEvent; +use Alchemy\Phrasea\Core\Event\ExportMailEvent; use Alchemy\Phrasea\Core\PhraseaEvents; use Alchemy\Phrasea\Exception\InvalidArgumentException; use Alchemy\Phrasea\Model\Manipulator\TokenManipulator; @@ -193,42 +194,26 @@ class ExportController extends Controller $token = $this->getTokenManipulator()->createEmailExportToken(serialize($list)); if (count($destMails) > 0) { - //zip documents - \set_export::build_zip( - $this->app, - $token, - $list, - $this->app['tmp.download.path'].'/'. $token->getValue() . '.zip' - ); + $emitterId = $this->getAuthenticatedUser()->getId(); - $remaingEmails = $destMails; + $tokenValue = $token->getValue(); $url = $this->app->url('prepare_download', ['token' => $token->getValue(), 'anonymous' => false, 'type' => \Session_Logger::EVENT_EXPORTMAIL]); - $user = $this->getAuthenticatedUser(); - $emitter = new Emitter($user->getDisplayName(), $user->getEmail()); + $params = [ + 'url' => $url, + 'textmail' => $request->request->get('textmail'), + 'reading_confirm' => !!$request->request->get('reading_confirm', false), + 'ssttid' => $ssttid = $request->request->get('ssttid', ''), + 'lst' => $lst = $request->request->get('lst', ''), + ]; - foreach ($destMails as $key => $mail) { - try { - $receiver = new Receiver(null, trim($mail)); - } catch (InvalidArgumentException $e) { - continue; - } - - $mail = MailRecordsExport::create($this->app, $receiver, $emitter, $request->request->get('textmail')); - $mail->setButtonUrl($url); - $mail->setExpiration($token->getExpiration()); - - $this->deliver($mail, !!$request->request->get('reading_confirm', false)); - unset($remaingEmails[$key]); - } - - //some mails failed - if (count($remaingEmails) > 0) { - foreach ($remaingEmails as $mail) { - $this->dispatch(PhraseaEvents::EXPORT_MAIL_FAILURE, new ExportFailureEvent($this->getAuthenticatedUser(), $ssttid, $lst, \eventsmanager_notify_downloadmailfail::MAIL_FAIL, $mail)); - } - } + $this->dispatch(PhraseaEvents::EXPORT_MAIL_CREATE, new ExportMailEvent( + $emitterId, + $tokenValue, + $destMails, + $params + )); } return $this->app->json([ diff --git a/lib/Alchemy/Phrasea/Core/Event/ExportMailEvent.php b/lib/Alchemy/Phrasea/Core/Event/ExportMailEvent.php new file mode 100644 index 0000000000..3acafb4c70 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/ExportMailEvent.php @@ -0,0 +1,53 @@ +emitterUserId = $emitterUserId; + $this->tokenValue = $tokenValue; + $this->destinationMails = $destMails; + $this->params = $params; + } + + public function getTokenValue() + { + return $this->tokenValue; + } + + public function getDestinationMails() + { + return $this->destinationMails; + } + + public function getEmitterUserId() + { + return $this->emitterUserId; + } + + public function getParams() + { + return $this->params; + } +} diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/ExportSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/ExportSubscriber.php index fca66dab4a..d3c05a64b6 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/ExportSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/ExportSubscriber.php @@ -12,7 +12,15 @@ namespace Alchemy\Phrasea\Core\Event\Subscriber; use Alchemy\Phrasea\Core\Event\ExportFailureEvent; +use Alchemy\Phrasea\Core\Event\ExportMailEvent; use Alchemy\Phrasea\Core\PhraseaEvents; +use Alchemy\Phrasea\Exception\InvalidArgumentException; +use Alchemy\Phrasea\Model\Entities\Token; +use Alchemy\Phrasea\Model\Repositories\TokenRepository; +use Alchemy\Phrasea\Model\Repositories\UserRepository; +use Alchemy\Phrasea\Notification\Emitter; +use Alchemy\Phrasea\Notification\Mail\MailRecordsExport; +use Alchemy\Phrasea\Notification\Receiver; class ExportSubscriber extends AbstractNotificationSubscriber { @@ -39,10 +47,65 @@ class ExportSubscriber extends AbstractNotificationSubscriber $this->app['event-manager']->notify($params['usr_id'], 'eventsmanager_notify_downloadmailfail', $datas, $mailed); } + public function onCreateExportMail(ExportMailEvent $event) + { + $destMails = $event->getDestinationMails(); + + $params = $event->getParams(); + + /** @var UserRepository $userRepository */ + $userRepository = $this->app['repo.users']; + + $user = $userRepository->find($event->getEmitterUserId()); + + /** @var TokenRepository $tokenRepository */ + $tokenRepository = $this->app['repo.tokens']; + + /** @var Token $token */ + $token = $tokenRepository->findValidToken($event->getTokenValue()); + + $list = unserialize($token->getData()); + + //zip documents + \set_export::build_zip( + $this->app, + $token, + $list, + $this->app['tmp.download.path'].'/'. $token->getValue() . '.zip' + ); + + $remaingEmails = $destMails; + + $emitter = new Emitter($user->getDisplayName(), $user->getEmail()); + + foreach ($destMails as $key => $mail) { + try { + $receiver = new Receiver(null, trim($mail)); + } catch (InvalidArgumentException $e) { + continue; + } + + $mail = MailRecordsExport::create($this->app, $receiver, $emitter, $params['textmail']); + $mail->setButtonUrl($params['url']); + $mail->setExpiration($token->getExpiration()); + + $this->deliver($mail, $params['reading_confirm']); + unset($remaingEmails[$key]); + } + + //some mails failed + if (count($remaingEmails) > 0) { + foreach ($remaingEmails as $mail) { + $this->app['dispatcher']->dispatch(PhraseaEvents::EXPORT_MAIL_FAILURE, new ExportFailureEvent($user, $params['ssttid'], $params['lst'], \eventsmanager_notify_downloadmailfail::MAIL_FAIL, $mail)); + } + } + } + public static function getSubscribedEvents() { return [ - PhraseaEvents::EXPORT_MAIL_FAILURE => 'onMailExportFailure' + PhraseaEvents::EXPORT_MAIL_FAILURE => 'onMailExportFailure', + PhraseaEvents::EXPORT_MAIL_CREATE => 'onCreateExportMail', ]; } } diff --git a/lib/Alchemy/Phrasea/Core/PhraseaEvents.php b/lib/Alchemy/Phrasea/Core/PhraseaEvents.php index 97c9c10ba5..90adac69dd 100644 --- a/lib/Alchemy/Phrasea/Core/PhraseaEvents.php +++ b/lib/Alchemy/Phrasea/Core/PhraseaEvents.php @@ -48,7 +48,8 @@ final class PhraseaEvents const BRIDGE_UPLOAD_FAILURE = 'bridge.upload-failure'; const EXPORT_MAIL_FAILURE = 'export.mail-failure'; - const EXPORT_CREATE = 'export.create'; + const EXPORT_CREATE = 'export.create'; + const EXPORT_MAIL_CREATE = 'export.mail-create'; const RECORD_EDIT = 'record.edit'; const RECORD_UPLOAD = 'record.upload'; diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/ExportTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/ExportTest.php index da278639c2..925caa9945 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/ExportTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/ExportTest.php @@ -165,7 +165,8 @@ class ExportTest extends \PhraseanetAuthenticatedWebTestCase */ public function testExportMail() { - $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRecordsExport'); + // deliver method removed in the listener +// $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRecordsExport'); $this->getClient()->request('POST', '/prod/export/mail/', [ 'lst' => $this->getRecord1()->getId(),