From 94a27e13c41c6e927fc685f359b9b0c59c9789ef Mon Sep 17 00:00:00 2001 From: Ansley Date: Thu, 24 Oct 2024 16:59:01 +0800 Subject: [PATCH] experiment with mailer and event driven --- .env.dist | 4 + compose.override.yaml | 12 ++ composer.json | 1 + composer.lock | 237 ++++++++++++++++++++- config/packages/mailer.yaml | 3 + config/services.yaml | 9 + src/Controller/EntityController.php | 16 ++ src/Event/ProductAdded.php | 21 ++ src/EventListener/ProductAddedListener.php | 48 +++++ symfony.lock | 12 ++ 10 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 compose.override.yaml create mode 100644 config/packages/mailer.yaml create mode 100644 src/Event/ProductAdded.php create mode 100644 src/EventListener/ProductAddedListener.php diff --git a/.env.dist b/.env.dist index 3919b7e8..5b77d485 100644 --- a/.env.dist +++ b/.env.dist @@ -90,3 +90,7 @@ HUB_JO_KEY=hub_jo_count # hub geofence HUB_GEOFENCE_ENABLE=set_to_true_or_false HUB_FILTER_ENABLE=set_to_true_or_false + +###> symfony/mailer ### +# MAILER_DSN=null://null +###< symfony/mailer ### diff --git a/compose.override.yaml b/compose.override.yaml new file mode 100644 index 00000000..8f2bb3d4 --- /dev/null +++ b/compose.override.yaml @@ -0,0 +1,12 @@ + +services: +###> symfony/mailer ### + mailer: + image: axllent/mailpit + ports: + - "1025" + - "8025" + environment: + MP_SMTP_AUTH_ACCEPT_ANY: 1 + MP_SMTP_AUTH_ALLOW_INSECURE: 1 +###< symfony/mailer ### diff --git a/composer.json b/composer.json index 8516749e..e07bdd8d 100644 --- a/composer.json +++ b/composer.json @@ -50,6 +50,7 @@ "symfony/filesystem": "^4.0", "symfony/flex": "^1.0", "symfony/framework-bundle": "^4.0", + "symfony/mailer": "^4.0", "symfony/maker-bundle": "^1.0", "symfony/monolog-bundle": "^3.7", "symfony/process": "^4.0", diff --git a/composer.lock b/composer.lock index 5914c598..1f9caf23 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "da765755121aaeb5c5a43aa74d7eeb76", + "content-hash": "3da24a255bf74ac8f6423361afa08210", "packages": [ { "name": "composer/package-versions-deprecated", @@ -1827,6 +1827,73 @@ }, "time": "2023-07-19T09:04:27+00:00" }, + { + "name": "egulias/email-validator", + "version": "3.2.6", + "source": { + "type": "git", + "url": "https://github.com/egulias/EmailValidator.git", + "reference": "e5997fa97e8790cdae03a9cbd5e78e45e3c7bda7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/egulias/EmailValidator/zipball/e5997fa97e8790cdae03a9cbd5e78e45e3c7bda7", + "reference": "e5997fa97e8790cdae03a9cbd5e78e45e3c7bda7", + "shasum": "" + }, + "require": { + "doctrine/lexer": "^1.2|^2", + "php": ">=7.2", + "symfony/polyfill-intl-idn": "^1.15" + }, + "require-dev": { + "phpunit/phpunit": "^8.5.8|^9.3.3", + "vimeo/psalm": "^4" + }, + "suggest": { + "ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Egulias\\EmailValidator\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Eduardo Gulias Davis" + } + ], + "description": "A library for validating emails against several RFCs", + "homepage": "https://github.com/egulias/EmailValidator", + "keywords": [ + "email", + "emailvalidation", + "emailvalidator", + "validation", + "validator" + ], + "support": { + "issues": "https://github.com/egulias/EmailValidator/issues", + "source": "https://github.com/egulias/EmailValidator/tree/3.2.6" + }, + "funding": [ + { + "url": "https://github.com/egulias", + "type": "github" + } + ], + "time": "2023-06-01T07:04:22+00:00" + }, { "name": "friendsofphp/proxy-manager-lts", "version": "v1.0.5", @@ -4867,6 +4934,87 @@ ], "time": "2021-11-22T14:10:53+00:00" }, + { + "name": "symfony/mailer", + "version": "v4.4.49", + "source": { + "type": "git", + "url": "https://github.com/symfony/mailer.git", + "reference": "554b8c0dc2db9d74e760fd6b726f527364f03302" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/mailer/zipball/554b8c0dc2db9d74e760fd6b726f527364f03302", + "reference": "554b8c0dc2db9d74e760fd6b726f527364f03302", + "shasum": "" + }, + "require": { + "egulias/email-validator": "^2.1.10|^3", + "php": ">=7.1.3", + "psr/log": "^1|^2|^3", + "symfony/event-dispatcher": "^4.3", + "symfony/mime": "^4.4.21|^5.2.6", + "symfony/polyfill-php80": "^1.16", + "symfony/service-contracts": "^1.1|^2" + }, + "conflict": { + "symfony/http-kernel": "<4.4", + "symfony/sendgrid-mailer": "<4.4" + }, + "require-dev": { + "symfony/amazon-mailer": "^4.4|^5.0", + "symfony/google-mailer": "^4.4|^5.0", + "symfony/http-client-contracts": "^1.1|^2", + "symfony/mailchimp-mailer": "^4.4|^5.0", + "symfony/mailgun-mailer": "^4.4|^5.0", + "symfony/messenger": "^4.4|^5.0", + "symfony/postmark-mailer": "^4.4|^5.0", + "symfony/sendgrid-mailer": "^4.4|^5.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Mailer\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Helps sending emails", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/mailer/tree/v4.4.49" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-04T06:30:35+00:00" + }, { "name": "symfony/maker-bundle", "version": "v1.36.3", @@ -4955,6 +5103,91 @@ ], "time": "2021-11-22T18:44:03+00:00" }, + { + "name": "symfony/mime", + "version": "v5.4.43", + "source": { + "type": "git", + "url": "https://github.com/symfony/mime.git", + "reference": "a02711d6ce461edada8c0f8641aa536709b99b47" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/mime/zipball/a02711d6ce461edada8c0f8641aa536709b99b47", + "reference": "a02711d6ce461edada8c0f8641aa536709b99b47", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/polyfill-intl-idn": "^1.10", + "symfony/polyfill-mbstring": "^1.0", + "symfony/polyfill-php80": "^1.16" + }, + "conflict": { + "egulias/email-validator": "~3.0.0", + "phpdocumentor/reflection-docblock": "<3.2.2", + "phpdocumentor/type-resolver": "<1.4.0", + "symfony/mailer": "<4.4", + "symfony/serializer": "<5.4.35|>=6,<6.3.12|>=6.4,<6.4.3" + }, + "require-dev": { + "egulias/email-validator": "^2.1.10|^3.1|^4", + "phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0", + "symfony/dependency-injection": "^4.4|^5.0|^6.0", + "symfony/process": "^5.4|^6.4", + "symfony/property-access": "^4.4|^5.1|^6.0", + "symfony/property-info": "^4.4|^5.1|^6.0", + "symfony/serializer": "^5.4.35|~6.3.12|^6.4.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Mime\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Allows manipulating MIME messages", + "homepage": "https://symfony.com", + "keywords": [ + "mime", + "mime-type" + ], + "support": { + "source": "https://github.com/symfony/mime/tree/v5.4.43" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-08-13T10:38:38+00:00" + }, { "name": "symfony/monolog-bridge", "version": "v5.2.12", @@ -7971,6 +8204,6 @@ "php": "^7.1.3", "ext-iconv": "*" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/config/packages/mailer.yaml b/config/packages/mailer.yaml new file mode 100644 index 00000000..56a650d8 --- /dev/null +++ b/config/packages/mailer.yaml @@ -0,0 +1,3 @@ +framework: + mailer: + dsn: '%env(MAILER_DSN)%' diff --git a/config/services.yaml b/config/services.yaml index 6af4a5d3..aa4046ae 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -33,6 +33,8 @@ services: resource: '../src/*' exclude: '../src/{Entity,Migrations,Tests,Menu,Access}' + + # controllers are imported separately to make sure services can be injected # as action arguments even if you don't extend any base controller class App\Controller\: @@ -169,6 +171,13 @@ services: #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Bing" App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet" #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google" + + App\EventListener\ProductAddedListener: + arguments: + $mailer: '@mailer' # Autowire the mailer service + $entityManager: '@doctrine.orm.entity_manager' # Autowire the EntityManager + tags: + - { name: 'kernel.event_listener', event: 'App\Event\ProductAdded', method: 'onProductAdded' } App\EventListener\JobOrderActiveCacheListener: arguments: diff --git a/src/Controller/EntityController.php b/src/Controller/EntityController.php index 1ed4509c..f7880b95 100644 --- a/src/Controller/EntityController.php +++ b/src/Controller/EntityController.php @@ -3,6 +3,7 @@ namespace App\Controller; use App\Entity\Entity; +use App\Event\ProductAdded; use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\EntityManagerInterface; @@ -10,6 +11,8 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; + use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; @@ -29,6 +32,13 @@ class EntityController extends Controller{ return $this->render('entity/list.html.twig'); } + private $eventDispatcher; + + public function __construct(EventDispatcherInterface $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + /** * @Menu(selected="entity_list") * @IsGranted("entity.add") @@ -78,10 +88,14 @@ class EntityController extends Controller{ $em->persist($test); $em->flush(); + $this->eventDispatcher->dispatch(new ProductAdded($test)); + // return successful response return $this->json([ 'success' => 'Changes have been saved!' ]); + + } @@ -129,6 +143,8 @@ class EntityController extends Controller{ // validated! save the entity $em->flush(); + $this->eventDispatcher->dispatch(new ProductAdded($e)); + // return successful response return $this->json([ 'success' => 'Changes have been saved!' diff --git a/src/Event/ProductAdded.php b/src/Event/ProductAdded.php new file mode 100644 index 00000000..42cf68e9 --- /dev/null +++ b/src/Event/ProductAdded.php @@ -0,0 +1,21 @@ +product = $product; + } + + public function getProduct(): Entity + { + return $this->product; + } +} diff --git a/src/EventListener/ProductAddedListener.php b/src/EventListener/ProductAddedListener.php new file mode 100644 index 00000000..b813b5bc --- /dev/null +++ b/src/EventListener/ProductAddedListener.php @@ -0,0 +1,48 @@ +mailer = $mailer; + $this->entityManager = $entityManager; + } + + public function onProductAdded(ProductAdded $event) + { + // Get all products in the database + $items = $this->entityManager->getRepository('App:Entity')->findAll(); + + // Convert to JSON format + $itemData = []; + foreach ($items as $item) { + $itemData[] = [ + 'id' => $item->getId(), + 'item' => $item->getItem(), + 'price' => $item->getPrice(), + 'description' => $item->getDescription(), + ]; + } + $jsonItems = json_encode($itemData, JSON_PRETTY_PRINT); + + // Send an email with the JSON data + $email = (new Email()) + ->from('ansleytheking@gmail.coml') + ->to('ansley.sze@foodgroup.ph') + ->subject('New Product Added - Item List in JSON') + ->text('A new product has been added. Here is the complete list of items in JSON format.') + ->attach($jsonItems, 'items.json', 'application/json'); + + $this->mailer->send($email); + } +} \ No newline at end of file diff --git a/symfony.lock b/symfony.lock index 82c192d2..d88ade54 100644 --- a/symfony.lock +++ b/symfony.lock @@ -242,6 +242,18 @@ "symfony/inflector": { "version": "v4.0.2" }, + "symfony/mailer": { + "version": "4.4", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "4.3", + "ref": "df66ee1f226c46f01e85c29c2f7acce0596ba35a" + }, + "files": [ + "config/packages/mailer.yaml" + ] + }, "symfony/maker-bundle": { "version": "1.0", "recipe": {