diff --git a/.env b/.env index 4a406fd..a122b5e 100644 --- a/.env +++ b/.env @@ -37,3 +37,5 @@ DATABASE_URL="mysql://gandalf:frodon@db:3306/mordor?serverVersion=5.7" # Delivery is disabled by default via "null://localhost" MAILER_URL=null://localhost ###< symfony/swiftmailer-bundle ### + +APP_MAIL_SENDTO= diff --git a/.env.dist b/.env.dist index 9e7162f..27fcd0f 100644 --- a/.env.dist +++ b/.env.dist @@ -4,3 +4,4 @@ APP_SECRET='$ecretf0rt3st' SYMFONY_DEPRECATIONS_HELPER=999999 PANTHER_APP_ENV=panther PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots +APP_MAIL_SENDTO= diff --git a/Makefile b/Makefile index 1e76f82..ea3f6c3 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ build: ## Build docker printf "\033[32m Deploy to Server \033[0m\n" $(DOCKER_COMPOSE) build -start: build ## Run project +run: build ## Run project printf " 🏃\033[33m Running application ... \033[0m\n" $(DOCKER_COMPOSE) pull $(DOCKER_COMPOSE) up -d @@ -69,6 +69,8 @@ stop: ## Stop the VMs $(DOCKER_COMPOSE) stop printf "\n\n" +start: run vendor assets ## Run project + ##.env.dist => .env.local CP ## diff --git a/composer.json b/composer.json index fc532b2..bee2d2f 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "symfony/serializer": "5.3.*", "symfony/string": "5.3.*", "symfony/swiftmailer-bundle": "^3.5", + "symfony/templating": "5.3.*", "symfony/translation": "5.3.*", "symfony/twig-bundle": "5.3.*", "symfony/validator": "5.3.*", @@ -43,8 +44,7 @@ "symfony/webpack-encore-bundle": "^1.12", "symfony/yaml": "5.3.*", "twig/extra-bundle": "^2.12|^3.0", - "twig/twig": "^2.12|^3.0", - "ext-http": "*" + "twig/twig": "^2.12|^3.0" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/composer.lock b/composer.lock index 6e3f720..e8742cd 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": "f080a06e459171c75918b8fece5db08a", + "content-hash": "3ef24f08a791f788d87879d9679391d3", "packages": [ { "name": "composer/package-versions-deprecated", @@ -6771,6 +6771,74 @@ ], "time": "2021-01-25T17:31:39+00:00" }, + { + "name": "symfony/templating", + "version": "v5.3.7", + "source": { + "type": "git", + "url": "https://github.com/symfony/templating.git", + "reference": "768d2fc5a9c9c93aad8c10af834a89f9d9d31011" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/templating/zipball/768d2fc5a9c9c93aad8c10af834a89f9d9d31011", + "reference": "768d2fc5a9c9c93aad8c10af834a89f9d9d31011", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "~1.8" + }, + "require-dev": { + "psr/log": "^1|^2|^3" + }, + "suggest": { + "psr/log-implementation": "For using debug logging in loaders" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Templating\\": "" + }, + "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": "Provides all the tools needed to build any kind of template system", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/templating/tree/v5.3.7" + }, + "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": "2021-08-04T21:20:46+00:00" + }, { "name": "symfony/translation", "version": "v5.3.7", @@ -9437,7 +9505,6 @@ "type": "github" } ], - "abandoned": true, "time": "2020-09-28T06:45:17+00:00" }, { diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index f9f4cc5..800f23c 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -1,5 +1,6 @@ twig: default_path: '%kernel.project_dir%/templates' + form_themes: ['form/theme/custom.html.twig'] when@test: twig: diff --git a/config/services.yaml b/config/services.yaml index ef07b76..e2ec96a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -4,6 +4,7 @@ # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: + app.mail.sendto: '%env(APP_MAIL_SENDTO)%' services: # default configuration for services in *this* file diff --git a/src/Controller/DefaultControler.php b/src/Controller/DefaultControler.php index 39639a1..19b5fc0 100644 --- a/src/Controller/DefaultControler.php +++ b/src/Controller/DefaultControler.php @@ -4,6 +4,7 @@ namespace App\Controller; use App\Form\Type\ContactType; +use App\Service\Mailer; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -13,15 +14,18 @@ class DefaultControler extends AbstractController { /** * @Route("/", name="index") + * + * @param Request $request + * @param Mailer $mailer + * @return Response */ - public function index(Request $request, \Swift_Mailer $mailer): Response + public function index(Request $request, Mailer $mailer): Response { $form = $this->createForm(ContactType::class, null); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $data = $form->getData(); - if($this->sendEmail($data, $mailer)) { + if($mailer->sendEmail($form->getData())) { $this->addFlash('contact', 'Votre email a Ă©tĂ© envoyĂ© et sera traitĂ© dans les meilleurs dĂ©lais.'); } else { $this->addFlash('echec', 'Un problĂšme a eu lieu durant l\'envoie, veuillez rĂ©-essayer plus tard'); @@ -32,35 +36,4 @@ public function index(Request $request, \Swift_Mailer $mailer): Response 'form' => $form->createView(), ]); } - - /** - * @param $data - * @param \Swift_Mailer $mailer - * - * @return bool - * @todo New service for that - */ - private function sendEmail($data, \Swift_Mailer $mailer) - { - $mail = new \Swift_Message(); - $mail - -> setSubject($data['objet']) - -> setFrom($data['email']) - -> setTo('emailsymfony5@gmail.com') - -> setBody( - $this -> renderView('emails/contact.html.twig', [ - 'data' => $data - ]), 'text/html' - ) - ; - - if($mailer -> send($mail)) - { - return true; - } - else - { - return false; - } - } } \ No newline at end of file diff --git a/src/Form/Type/ContactType.php b/src/Form/Type/ContactType.php index 03bf4ab..818cde0 100644 --- a/src/Form/Type/ContactType.php +++ b/src/Form/Type/ContactType.php @@ -16,11 +16,11 @@ class ContactType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('name', TextType::class) - ->add('email', TextType::class) - ->add('subject', TextType::class) - ->add('message', TextareaType::class) - ->add('send', SubmitType::class) + ->add('name', TextType::class, ['label' => 'Nom']) + ->add('email', TextType::class, ['label' => 'E-mail']) + ->add('subject', TextType::class, ['label' => 'Sujet']) + ->add('message', TextareaType::class, ['label' => 'Message']) + ->add('send', SubmitType::class, ['label' => 'Envoyer']) ; } diff --git a/src/Service/Mailer.php b/src/Service/Mailer.php index 15f830d..082a248 100644 --- a/src/Service/Mailer.php +++ b/src/Service/Mailer.php @@ -3,41 +3,44 @@ namespace App\Service; +use Twig\Environment; + class Mailer { - public function __construct() - { + /** @var Environment */ + private $twig; + + /** @var \Swift_Mailer */ + private $mailer; + public function __construct(Environment $twig, \Swift_Mailer $mailer) + { + $this->twig = $twig; + $this->mailer = $mailer; } /** - * @param $data - * @param \Swift_Mailer $mailer - * + * @param $data * @return bool - * @todo New service for that */ - private function sendEmail($data, \Swift_Mailer $mailer) + public function sendEmail($data) { $mail = new \Swift_Message(); $mail - -> setSubject($data['objet']) - -> setFrom($data['email']) - -> setTo('emailsymfony5@gmail.com') - -> setBody( - $this->renderView('emails/contact.html.twig', [ + ->setSubject($data['objet']) + ->setFrom($data['email']) + ->setTo('emailsymfony5@gmail.com') + ->setBody( + $this->twig->render('emails/contact.html.twig', [ 'data' => $data ]), 'text/html' ) ; - if($mailer -> send($mail)) - { - return true; - } - else - { + if (0 === $this->mailer->send($mail)){ return false; } + + return true; } } \ No newline at end of file diff --git a/symfony.lock b/symfony.lock index f328d40..b942478 100644 --- a/symfony.lock +++ b/symfony.lock @@ -526,6 +526,9 @@ "config/packages/test/swiftmailer.yaml" ] }, + "symfony/templating": { + "version": "v5.3.7" + }, "symfony/translation": { "version": "5.3", "recipe": { diff --git a/templates/base.html.twig b/templates/base.html.twig index 3a74dae..b4046c9 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -213,7 +213,6 @@ {% endblock %} {% block contact_form %} - {% endblock %}
Un renseignement ? @@ -245,6 +244,8 @@
+ {% endblock %} +