<?php
namespace App\Auctioneer;
use App\Entity\Auction;
use App\Entity\PrivateUser;
use App\Entity\ProUser;
use App\Entity\Sale;
use App\Entity\User;
use App\Entity\Vehicle;
use App\Mailer\TranslatableMailer;
use App\Utils\Price;
use Doctrine\ORM\EntityManager;
use Twig\Environment;
/**
* Responsible for sending emails to bidders.
*/
class Notifier
{
public const EMAIL_SUPPORT_VPAUTO = ['support-vpauto-vente@vpauto.fr'];
protected $mailer;
protected $templating;
protected $em;
public function __construct(TranslatableMailer $mailer, Environment $templating, protected $from, EntityManager $em)
{
$this->mailer = $mailer;
$this->templating = $templating;
$this->em = $em;
}
public function sendNotifications(FilterBiddingResultEvent $event): void
{
$result = $event->getBiddingResult();
$auction = $event->getAuction();
$vehicle = $auction->getVehicle();
$bid = $result->getBid();
$highestBid = $result->getHighestBid();
if (!$auction->isOpen() && BiddingResult::IMMEDIATE_WIN !== $result->getCode()) {
return;
}
if (BiddingResult::SUCCESS === $result->getCode()) {
if (null !== $bid) {
if (null !== $highestBid) {
if ($bid->getUser() !== $highestBid->getUser()) {
$this->mailer->send(
$highestBid->getUser(),
$highestBid->getUser() instanceof ProUser ? '/Mail/bid_lost_pro.html.twig' : '/Mail/bid_lost.html.twig',
[
'amount' => Price::getCurrentAuction($vehicle),
'bid' => $bid,
'vehicle' => $vehicle,
'user' => $highestBid->getUser(),
'auction' => $auction,
]
);
} elseif (Sale::OPTION_GP === $vehicle->getSale()->getPublic() and $bid->getUser() instanceof PrivateUser) {
$this->mailer->send(
$bid->getUser(),
'/Mail/offer_new.html.twig',
[
'vehicle' => $vehicle,
'bid' => $bid,
]
);
}
}
}
} elseif (BiddingResult::IMMEDIATE_WIN === $result->getCode()) {
foreach ($auction->getUsers() as $user) {
if ($bid->getUser() === $user) {
$this->mailer->send(
$user,
'/Mail/immediate_purchase_win.html.twig',
[
'bid' => $bid,
'user' => $user,
'vehicle' => $auction->getVehicle(),
]
);
} else {
$this->mailer->send(
$user,
'/Mail/immediate_purchase_lost.html.twig',
[
'user' => $user,
'vehicle' => $auction->getVehicle(),
]
);
}
}
}
}
public function sendFinalNotifications($sale): void
{
$results = [];
foreach ($sale->getVehicles() as $vehicle) {
if (!$vehicle->hasOffer()) {
continue;
}
$auction = $vehicle->getAuction();
$highestBid = $this->em->getRepository(\App\Entity\Bid::class)->findHighestBidByAuction($auction);
foreach ($auction->getUsers() as $user) {
if (!isset($results[$user->getEmail()]['user'])) {
$results[$user->getEmail()]['user'] = $user;
}
if ($auction->getWinner() === $user && $vehicle->isReserveReached()) {
$results[$user->getEmail()]['won'][] = $vehicle;
} elseif ($highestBid->getUser() === $user && !$vehicle->isReserveReached()) {
$results[$user->getEmail()]['waiting'][] = $vehicle;
} else {
$results[$user->getEmail()]['lost'][] = $vehicle;
}
}
}
foreach ($results as $mail => $result) {
if (isset($result['won'])) {
$this->mailer->send(
$result['user'],
'/Mail/auction_won.html.twig',
[
'sale' => $sale,
'user' => $result['user'],
'vehiclesWon' => $result['won'],
'vehiclesWait' => $result['waiting'] ?? null,
'sameCountry' => $sale->isSameCountry($result['user']),
],
null,
null,
null,
self::EMAIL_SUPPORT_VPAUTO
);
} elseif (isset($result['waiting'])) {
$this->mailer->send(
$result['user'],
'/Mail/auction_waiting.html.twig',
[
'sale' => $sale,
'user' => $result['user'],
'vehiclesWait' => $result['waiting'],
],
null,
null,
null,
self::EMAIL_SUPPORT_VPAUTO
);
} else {
$this->mailer->send(
$result['user'],
'/Mail/auction_lost.html.twig',
[
'sale' => $sale,
'user' => $result['user'],
'vehiclesLost' => $result['lost'],
]
);
}
}
}
public function sendNotificationToLosers($sale): void
{
$results = [];
foreach ($sale->getVehicles() as $vehicle) {
if (!$vehicle->hasOffer()) {
continue;
}
$auction = $vehicle->getAuction();
foreach ($auction->getUsers() as $user) {
if ($auction->getWinner() == $user && Vehicle::SALING_STATE_RETIRE === $vehicle->getSalingState()) {
if (!isset($results[$user->getEmail()]['user'])) {
$results[$user->getEmail()]['user'] = $user;
}
$results[$user->getEmail()]['vehicles'][] = $vehicle;
}
}
}
foreach ($results as $result) {
$this->mailer->send(
$result['user'],
'/Mail/auction_lost_not_exclusive.html.twig',
[
'sale' => $sale,
'user' => $result['user'],
'vehicles' => $result['vehicles'],
]
);
}
}
}