FreightOrders/tests/Fakes/Carrier/FakeCarrierRepository.php
Yisroel Baum 948380ba9f
remove references of carrier and freight order ids from each other
They reference each other in created bid so these arrays are not necessary
2025-11-18 09:08:15 +02:00

76 lines
2 KiB
PHP

<?php
namespace Tests\Fakes\Carrier;
use FreightQuote\Carrier\Carrier;
use FreightQuote\Carrier\CarrierRepository;
class FakeCarrierRepository implements CarrierRepository
{
/**
* @var Carrier[]
*/
private array $existingCarriers = [];
public function find(int $id): ?Carrier
{
foreach ($this->existingCarriers as $carrier) {
if ($carrier->getId() === $id) {
return new Carrier(
$id,
$carrier->getEmail(),
$carrier->getCompanyName(),
$carrier->getContactPerson(),
$carrier->getPhoneNumber(),
$carrier->getNotes(),
$carrier->getLoadProfile(),
$carrier->getCountriesServing(),
);
}
}
return null;
}
public function save(Carrier $carrier): Carrier
{
$id = $carrier->getId();
if ($id === null) {
$id = $this->autoIncrementId();
$carrier->setId($id);
}
$this->existingCarriers[$id] = $carrier;
return new Carrier(
$id,
$carrier->getEmail(),
$carrier->getCompanyName(),
$carrier->getContactPerson(),
$carrier->getPhoneNumber(),
$carrier->getNotes(),
$carrier->getLoadProfile(),
$carrier->getCountriesServing(),
);
}
private function autoIncrementId(): int
{
return count($this->existingCarriers);
}
public function getAll(): array
{
return array_map(function (Carrier $carrier) {
return new Carrier(
$carrier->getId(),
$carrier->getEmail(),
$carrier->getCompanyName(),
$carrier->getContactPerson(),
$carrier->getPhoneNumber(),
$carrier->getNotes(),
$carrier->getLoadProfile(),
$carrier->getCountriesServing(),
);
}, $this->existingCarriers);
}
}