From e88fcf02b1c6ef07e32ae7ac4900f641198aedc6 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 27 Aug 2019 08:57:54 +0200 Subject: [PATCH] Add email delivery for testing purposes In order to demonstrate mailhog integration into codeception. Also refactor logic into signal / slot to not bloat controller. --- .../Classes/Controller/AddressController.php | 12 ++-- .../Classes/Domain/Event/Address.php | 58 +++++++++++++++++++ .../example_extension/ext_localconf.php | 7 +++ 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 localPackages/example_extension/Classes/Domain/Event/Address.php diff --git a/localPackages/example_extension/Classes/Controller/AddressController.php b/localPackages/example_extension/Classes/Controller/AddressController.php index 1d75f28..04bf58f 100644 --- a/localPackages/example_extension/Classes/Controller/AddressController.php +++ b/localPackages/example_extension/Classes/Controller/AddressController.php @@ -55,13 +55,11 @@ class AddressController extends ActionController { $this->addressRepository->update($address); - $this->addFlashMessage( - LocalizationUtility::translate('flashSuccess', 'ExampleExtension', [ - 'companyName' => $address->getCompanyName(), - 'street' => $address->getStreet(), - ]), - 'Update successfully' - ); + $this->signalSlotDispatcher->dispatch(__CLASS__, 'addressUpdated', [ + $address, + $this, + ]); + $this->redirect('index'); } } diff --git a/localPackages/example_extension/Classes/Domain/Event/Address.php b/localPackages/example_extension/Classes/Domain/Event/Address.php new file mode 100644 index 0000000..3dd9b62 --- /dev/null +++ b/localPackages/example_extension/Classes/Domain/Event/Address.php @@ -0,0 +1,58 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use TYPO3\CMS\Core\Mail\MailMessage; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Utility\LocalizationUtility; +use Workshop\ExampleExtension\Controller\AddressController; +use Workshop\ExampleExtension\Domain\Model\Address as AddressModel; + +class Address +{ + public function updated(AddressModel $address, AddressController $controller) + { + $this->sendUpdateEMail($address); + $this->addUpdateFlashMessage($address, $controller); + } + + private function sendUpdateEmail(AddressModel $address) + { + $mail = GeneralUtility::makeInstance(MailMessage::class); + + $mail->setSubject('Address ' . $address->getCompanyName() . ' was updated'); + $mail->setTo(array('coding@daniel-siepmann.de')); + $mail->setBody('The address ' . $address->getCompanyName() . ' was successfully updated.'); + $mail->send(); + } + + private function addUpdateFlashMessage(AddressModel $address, AddressController $controller) + { + $controller->addFlashMessage( + LocalizationUtility::translate('flashSuccess', 'ExampleExtension', [ + 'companyName' => $address->getCompanyName(), + 'street' => $address->getStreet(), + ]), + 'Update successfully' + ); + } +} diff --git a/localPackages/example_extension/ext_localconf.php b/localPackages/example_extension/ext_localconf.php index addac49..c4df2d8 100644 --- a/localPackages/example_extension/ext_localconf.php +++ b/localPackages/example_extension/ext_localconf.php @@ -41,4 +41,11 @@ } } '); + + \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class)->connect( + \Workshop\ExampleExtension\Controller\AddressController::class, + 'addressUpdated', + \Workshop\ExampleExtension\Domain\Event\Address::class, + 'updated' + ); })();