<?php
namespace App\EventListener;
use App\Auctioneer\BiddingResult;
use App\Auctioneer\FilterBiddingResultEvent;
use App\Entity\Sale;
use Psr\Log\LoggerInterface;
class BidLogListener
{
private $logger;
public function __construct(LoggerInterface $bidsLogger)
{
$this->logger = $bidsLogger;
}
public function onNewBid(FilterBiddingResultEvent $event): void
{
$result = $event->getBiddingResult();
$auction = $event->getAuction();
$sale = $auction->getVehicle()->getSale();
$bid = $result->getBid();
if (
null === $bid
|| Sale::TYPE_ONLINE !== $sale->getType()
|| in_array($result->getCode(), $this->getExludedBiddingResultsCodes())
) {
return;
}
$this->logger->info(
sprintf(
'vehicle: %s, user: %s, amount: %s, status: %s, origin: %s, timestamp: %s | winner: %s, finalAmount: %d',
$auction->getVehicle()->getId(),
$bid->getUser(),
$bid->getAmount(),
$this->getBinddingResultLabel($result),
$bid->getOrigin(),
$bid->getTimestamp(),
$auction->getWinner()->getEmail(),
$auction->getWinningBidAmount()
)
);
}
private function getExludedBiddingResultsCodes()
{
return [
BiddingResult::AMOUNT_INVALID,
BiddingResult::AMOUNT_TOO_LOW,
BiddingResult::AMOUNT_JUST_BEATEN,
BiddingResult::BID_TOO_LATE,
];
}
private function getBinddingResultLabel(BiddingResult $result)
{
return match ($result->getCode()) {
BiddingResult::AMOUNT_BEATEN => 'Dépassé',
BiddingResult::IMMEDIATE_WIN => 'Achat immédiat',
BiddingResult::SUCCESS => 'Succès',
default => 'NC',
};
}
}