experiment with mailer and event driven

This commit is contained in:
Ansley 2024-10-24 16:59:01 +08:00
parent 9ea0224880
commit 94a27e13c4
10 changed files with 361 additions and 2 deletions

View file

@ -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 ###

12
compose.override.yaml Normal file
View file

@ -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 ###

View file

@ -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",

237
composer.lock generated
View file

@ -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"
}

View file

@ -0,0 +1,3 @@
framework:
mailer:
dsn: '%env(MAILER_DSN)%'

View file

@ -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:

View file

@ -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!'

View file

@ -0,0 +1,21 @@
<?php
// src/Event/ProductAdded.php
namespace App\Event;
use Symfony\Contracts\EventDispatcher\Event;
use App\Entity\Entity;
class ProductAdded
{
private $product;
public function __construct(Entity $product)
{
$this->product = $product;
}
public function getProduct(): Entity
{
return $this->product;
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace App\EventListener;
use App\Event\ProductAdded;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class ProductAddedListener
{
private $mailer;
private $entityManager;
public function __construct(MailerInterface $mailer, EntityManagerInterface $entityManager)
{
$this->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);
}
}

View file

@ -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": {