src/Auctioneer/Notifier.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Auctioneer;
  3. use App\Entity\Auction;
  4. use App\Entity\PrivateUser;
  5. use App\Entity\ProUser;
  6. use App\Entity\Sale;
  7. use App\Entity\User;
  8. use App\Entity\Vehicle;
  9. use App\Mailer\TranslatableMailer;
  10. use App\Utils\Price;
  11. use Doctrine\ORM\EntityManager;
  12. use Twig\Environment;
  13. /**
  14.  * Responsible for sending emails to bidders.
  15.  */
  16. class Notifier
  17. {
  18.     public const EMAIL_SUPPORT_VPAUTO = ['support-vpauto-vente@vpauto.fr'];
  19.     protected $mailer;
  20.     protected $templating;
  21.     protected $em;
  22.     public function __construct(TranslatableMailer $mailerEnvironment $templating, protected $fromEntityManager $em)
  23.     {
  24.         $this->mailer $mailer;
  25.         $this->templating $templating;
  26.         $this->em $em;
  27.     }
  28.     public function sendNotifications(FilterBiddingResultEvent $event): void
  29.     {
  30.         $result $event->getBiddingResult();
  31.         $auction $event->getAuction();
  32.         $vehicle $auction->getVehicle();
  33.         $bid $result->getBid();
  34.         $highestBid $result->getHighestBid();
  35.         if (!$auction->isOpen() && BiddingResult::IMMEDIATE_WIN !== $result->getCode()) {
  36.             return;
  37.         }
  38.         if (BiddingResult::SUCCESS === $result->getCode()) {
  39.             if (null !== $bid) {
  40.                 if (null !== $highestBid) {
  41.                     if ($bid->getUser() !== $highestBid->getUser()) {
  42.                         $this->mailer->send(
  43.                             $highestBid->getUser(),
  44.                             $highestBid->getUser() instanceof ProUser '/Mail/bid_lost_pro.html.twig' '/Mail/bid_lost.html.twig',
  45.                             [
  46.                                 'amount' => Price::getCurrentAuction($vehicle),
  47.                                 'bid' => $bid,
  48.                                 'vehicle' => $vehicle,
  49.                                 'user' => $highestBid->getUser(),
  50.                                 'auction' => $auction,
  51.                             ]
  52.                         );
  53.                     } elseif (Sale::OPTION_GP === $vehicle->getSale()->getPublic() and $bid->getUser() instanceof PrivateUser) {
  54.                         $this->mailer->send(
  55.                             $bid->getUser(),
  56.                             '/Mail/offer_new.html.twig',
  57.                             [
  58.                                 'vehicle' => $vehicle,
  59.                                 'bid' => $bid,
  60.                             ]
  61.                         );
  62.                     }
  63.                 }
  64.             }
  65.         } elseif (BiddingResult::IMMEDIATE_WIN === $result->getCode()) {
  66.             foreach ($auction->getUsers() as $user) {
  67.                 if ($bid->getUser() === $user) {
  68.                     $this->mailer->send(
  69.                         $user,
  70.                         '/Mail/immediate_purchase_win.html.twig',
  71.                         [
  72.                             'bid' => $bid,
  73.                             'user' => $user,
  74.                             'vehicle' => $auction->getVehicle(),
  75.                         ]
  76.                     );
  77.                 } else {
  78.                     $this->mailer->send(
  79.                         $user,
  80.                         '/Mail/immediate_purchase_lost.html.twig',
  81.                         [
  82.                             'user' => $user,
  83.                             'vehicle' => $auction->getVehicle(),
  84.                         ]
  85.                     );
  86.                 }
  87.             }
  88.         }
  89.     }
  90.     public function sendFinalNotifications($sale): void
  91.     {
  92.         $results = [];
  93.         foreach ($sale->getVehicles() as $vehicle) {
  94.             if (!$vehicle->hasOffer()) {
  95.                 continue;
  96.             }
  97.             $auction $vehicle->getAuction();
  98.             $highestBid $this->em->getRepository(\App\Entity\Bid::class)->findHighestBidByAuction($auction);
  99.             foreach ($auction->getUsers() as $user) {
  100.                 if (!isset($results[$user->getEmail()]['user'])) {
  101.                     $results[$user->getEmail()]['user'] = $user;
  102.                 }
  103.                 if ($auction->getWinner() === $user && $vehicle->isReserveReached()) {
  104.                     $results[$user->getEmail()]['won'][] = $vehicle;
  105.                 } elseif ($highestBid->getUser() === $user && !$vehicle->isReserveReached()) {
  106.                     $results[$user->getEmail()]['waiting'][] = $vehicle;
  107.                 } else {
  108.                     $results[$user->getEmail()]['lost'][] = $vehicle;
  109.                 }
  110.             }
  111.         }
  112.         foreach ($results as $mail => $result) {
  113.             if (isset($result['won'])) {
  114.                 $this->mailer->send(
  115.                     $result['user'],
  116.                     '/Mail/auction_won.html.twig',
  117.                     [
  118.                         'sale' => $sale,
  119.                         'user' => $result['user'],
  120.                         'vehiclesWon' => $result['won'],
  121.                         'vehiclesWait' => $result['waiting'] ?? null,
  122.                         'sameCountry' => $sale->isSameCountry($result['user']),
  123.                     ],
  124.                     null,
  125.                     null,
  126.                     null,
  127.                     self::EMAIL_SUPPORT_VPAUTO
  128.                 );
  129.             } elseif (isset($result['waiting'])) {
  130.                 $this->mailer->send(
  131.                     $result['user'],
  132.                     '/Mail/auction_waiting.html.twig',
  133.                     [
  134.                         'sale' => $sale,
  135.                         'user' => $result['user'],
  136.                         'vehiclesWait' => $result['waiting'],
  137.                     ],
  138.                     null,
  139.                     null,
  140.                     null,
  141.                     self::EMAIL_SUPPORT_VPAUTO
  142.                 );
  143.             } else {
  144.                 $this->mailer->send(
  145.                     $result['user'],
  146.                     '/Mail/auction_lost.html.twig',
  147.                     [
  148.                         'sale' => $sale,
  149.                         'user' => $result['user'],
  150.                         'vehiclesLost' => $result['lost'],
  151.                     ]
  152.                 );
  153.             }
  154.         }
  155.     }
  156.     public function sendNotificationToLosers($sale): void
  157.     {
  158.         $results = [];
  159.         foreach ($sale->getVehicles() as $vehicle) {
  160.             if (!$vehicle->hasOffer()) {
  161.                 continue;
  162.             }
  163.             $auction $vehicle->getAuction();
  164.             foreach ($auction->getUsers() as $user) {
  165.                 if ($auction->getWinner() == $user && Vehicle::SALING_STATE_RETIRE === $vehicle->getSalingState()) {
  166.                     if (!isset($results[$user->getEmail()]['user'])) {
  167.                         $results[$user->getEmail()]['user'] = $user;
  168.                     }
  169.                     $results[$user->getEmail()]['vehicles'][] = $vehicle;
  170.                 }
  171.             }
  172.         }
  173.         foreach ($results as $result) {
  174.             $this->mailer->send(
  175.                 $result['user'],
  176.                 '/Mail/auction_lost_not_exclusive.html.twig',
  177.                 [
  178.                     'sale' => $sale,
  179.                     'user' => $result['user'],
  180.                     'vehicles' => $result['vehicles'],
  181.                 ]
  182.             );
  183.         }
  184.     }
  185. }