This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
pflaenz.li-Symfony/src/Controller/TradeController.php

55 lines
1.8 KiB
PHP
Raw Normal View History

2021-05-28 11:23:04 +02:00
<?php
namespace App\Controller;
2021-05-28 17:33:10 +02:00
use App\Entity\User;
use App\Entity\Offering;
2021-05-28 11:23:04 +02:00
use App\Repository\OfferingRepository;
use App\Repository\WishRepository;
2021-05-28 17:33:10 +02:00
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
2021-05-28 11:23:04 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
2021-06-17 17:24:53 +02:00
use Symfony\Component\Mime\Address;
2021-05-28 11:23:04 +02:00
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
2021-05-29 19:45:45 +02:00
2021-05-28 11:23:04 +02:00
class TradeController extends AbstractController
{
2022-01-17 21:25:31 +01:00
#[Route('/trade/{urlId}', name: 'trade')]
2021-06-06 11:20:51 +02:00
public function sendEmail(MailerInterface $mailer, Offering $offer, OfferingRepository $offeringRepository, WishRepository $wishRepository): Response
2021-05-28 11:23:04 +02:00
{
$user = $this->getUser();
2021-05-28 17:33:10 +02:00
if($user != $offer->getByUser())
2021-05-28 11:23:04 +02:00
{
2021-05-28 17:33:10 +02:00
$email = (new TemplatedEmail())
2021-06-17 17:24:53 +02:00
->from(new Address('no-reply@pflaenz.li', 'Pflänzli no-reply'))
2021-05-28 17:33:10 +02:00
->to($offer->getByUser()->getEmail())
->replyTo($user->getEmail())
->subject($user->getUsername() . ' wants to trade with you!')
->text('Trade offer')
->htmlTemplate('user/trade/offer_email.html.twig')
->context([
'user' => $user,
2022-01-17 21:25:31 +01:00
'urlId' => $user->getUrlId(),
2021-05-28 17:33:10 +02:00
])
;
try
{
$mailer->send($email);
$this->addFlash('success', 'Trade offer sent!');
} catch (TransportExceptionInterface $e) {
$this->addFlash('error', 'There was an error: ' . $e);
}
} else {
$this->addFlash('error','You can\'t trade with yourself!');
2021-05-28 11:23:04 +02:00
}
2021-05-28 17:33:10 +02:00
2022-01-17 21:25:31 +01:00
return $this->redirectToRoute('show_offer', ['urlId' => $offer->getUrlId()]);
2021-05-28 11:23:04 +02:00
}
}