src/EventListener/BidLogListener.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Auctioneer\BiddingResult;
  4. use App\Auctioneer\FilterBiddingResultEvent;
  5. use App\Entity\Sale;
  6. use Psr\Log\LoggerInterface;
  7. class BidLogListener
  8. {
  9.     private $logger;
  10.     public function __construct(LoggerInterface $bidsLogger)
  11.     {
  12.         $this->logger $bidsLogger;
  13.     }
  14.     public function onNewBid(FilterBiddingResultEvent $event): void
  15.     {
  16.         $result $event->getBiddingResult();
  17.         $auction $event->getAuction();
  18.         $sale $auction->getVehicle()->getSale();
  19.         $bid $result->getBid();
  20.         if (
  21.             null === $bid
  22.             || Sale::TYPE_ONLINE !== $sale->getType()
  23.             || in_array($result->getCode(), $this->getExludedBiddingResultsCodes())
  24.         ) {
  25.             return;
  26.         }
  27.         $this->logger->info(
  28.             sprintf(
  29.                 'vehicle: %s, user: %s, amount: %s, status: %s, origin: %s, timestamp: %s | winner: %s, finalAmount: %d',
  30.                 $auction->getVehicle()->getId(),
  31.                 $bid->getUser(),
  32.                 $bid->getAmount(),
  33.                 $this->getBinddingResultLabel($result),
  34.                 $bid->getOrigin(),
  35.                 $bid->getTimestamp(),
  36.                 $auction->getWinner()->getEmail(),
  37.                 $auction->getWinningBidAmount()
  38.             )
  39.         );
  40.     }
  41.     private function getExludedBiddingResultsCodes()
  42.     {
  43.         return [
  44.             BiddingResult::AMOUNT_INVALID,
  45.             BiddingResult::AMOUNT_TOO_LOW,
  46.             BiddingResult::AMOUNT_JUST_BEATEN,
  47.             BiddingResult::BID_TOO_LATE,
  48.         ];
  49.     }
  50.     private function getBinddingResultLabel(BiddingResult $result)
  51.     {
  52.         return match ($result->getCode()) {
  53.             BiddingResult::AMOUNT_BEATEN => 'Dépassé',
  54.             BiddingResult::IMMEDIATE_WIN => 'Achat immédiat',
  55.             BiddingResult::SUCCESS => 'Succès',
  56.             default => 'NC',
  57.         };
  58.     }
  59. }