FreightOrders/tests/Fakes/Bid/FakeBidRepository.php
Yisroel Baum e79064d909
change how ids are generated
from uniquid to hex of random bytes
2025-11-18 09:05:55 +02:00

54 lines
1.1 KiB
PHP

<?php
namespace Tests\Fakes\Bid;
use FreightQuote\Bid\Bid;
use FreightQuote\Bid\BidRepository;
class FakeBidRepository implements BidRepository
{
/**
* @var Bid[]
*/
private array $existingBids = [];
public function save(Bid $bid): Bid
{
$id = $bid->getId();
if ($id === null) {
$id = $this->getUniqueId();
$bid->setId($id);
}
$this->existingBids[$id] = $bid;
return new Bid(
$id,
$bid->getFreightOrderId(),
$bid->getCarrierId(),
);
}
public function find(string $id): ?Bid
{
foreach ($this->existingBids as $bid) {
if ($bid->getId() === $id) {
return new Bid(
$id,
$bid->getFreightOrderId(),
$bid->getCarrierId(),
);
}
}
return null;
}
private function getUniqueId(): string
{
$id = bin2hex(random_bytes(10));
while ($this->find($id) !== null) {
$id = bin2hex(random_bytes(10));
}
return $id;
}
}