mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 02:09:03 +02:00
Added tests for DTOConverter
This commit is contained in:
parent
de82249d8d
commit
8ea92ef330
11 changed files with 429 additions and 11 deletions
|
@ -23,12 +23,20 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
/**
|
||||
* This DTO represents a file that can be downloaded from a URL.
|
||||
* This could be a datasheet, a 3D model, a picture or similar.
|
||||
*/
|
||||
class FileDTO
|
||||
{
|
||||
/**
|
||||
* @param string $url The URL where to get this file
|
||||
* @param string|null $name Optionally the name of this file
|
||||
*/
|
||||
public function __construct(
|
||||
/** The URL where to get this file */
|
||||
public readonly string $url,
|
||||
/** Optionally the name of this file */
|
||||
public readonly ?string $name = null,
|
||||
) {}
|
||||
|
||||
|
||||
}
|
|
@ -23,6 +23,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
/**
|
||||
* This DTO represents a parameter of a part (similar to the AbstractParameter entity).
|
||||
* This could be a voltage, a current, a temperature or similar.
|
||||
*/
|
||||
class ParameterDTO
|
||||
{
|
||||
public function __construct(
|
||||
|
|
|
@ -24,8 +24,10 @@ declare(strict_types=1);
|
|||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
use App\Entity\Parts\ManufacturingStatus;
|
||||
use Hoa\Zformat\Parameter;
|
||||
|
||||
/**
|
||||
* This DTO represents a part with all its details.
|
||||
*/
|
||||
class PartDetailDTO extends SearchResultDTO
|
||||
{
|
||||
public function __construct(
|
||||
|
@ -43,6 +45,8 @@ class PartDetailDTO extends SearchResultDTO
|
|||
public readonly ?string $notes = null,
|
||||
/** @var FileDTO[]|null */
|
||||
public readonly ?array $datasheets = null,
|
||||
/** @var FileDTO[]|null */
|
||||
public readonly ?array $images = null,
|
||||
/** @var ParameterDTO[]|null */
|
||||
public readonly ?array $parameters = null,
|
||||
/** @var PurchaseInfoDTO[]|null */
|
||||
|
|
|
@ -46,6 +46,10 @@ class PriceDTO
|
|||
$this->price_as_big_decimal = BigDecimal::of($this->price);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the price as BigDecimal
|
||||
* @return BigDecimal
|
||||
*/
|
||||
public function getPriceAsBigDecimal(): BigDecimal
|
||||
{
|
||||
return $this->price_as_big_decimal;
|
||||
|
|
|
@ -23,6 +23,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\InfoProviderSystem\DTOs;
|
||||
|
||||
/**
|
||||
* This DTO represents a purchase information for a part (supplier name, order number and prices).
|
||||
*/
|
||||
class PurchaseInfoDTO
|
||||
{
|
||||
public function __construct(
|
||||
|
|
|
@ -25,6 +25,9 @@ namespace App\Services\InfoProviderSystem\DTOs;
|
|||
|
||||
use App\Entity\Parts\ManufacturingStatus;
|
||||
|
||||
/**
|
||||
* This DTO represents a search result for a part.
|
||||
*/
|
||||
class SearchResultDTO
|
||||
{
|
||||
public function __construct(
|
||||
|
|
|
@ -47,13 +47,21 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
/**
|
||||
* This class converts DTOs to entities which can be persisted in the DB
|
||||
*/
|
||||
class DTOtoEntityConverter
|
||||
final class DTOtoEntityConverter
|
||||
{
|
||||
private const TYPE_DATASHEETS_NAME = 'Datasheet';
|
||||
private const TYPE_IMAGE_NAME = 'Image';
|
||||
|
||||
public function __construct(private readonly EntityManagerInterface $em, private readonly string $base_currency)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given DTO to a PartParameter entity.
|
||||
* @param ParameterDTO $dto
|
||||
* @param PartParameter $entity The entity to apply the DTO on. If null a new entity will be created
|
||||
* @return PartParameter
|
||||
*/
|
||||
public function convertParameter(ParameterDTO $dto, PartParameter $entity = new PartParameter()): PartParameter
|
||||
{
|
||||
$entity->setName($dto->name);
|
||||
|
@ -68,6 +76,12 @@ class DTOtoEntityConverter
|
|||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given DTO to a Pricedetail entity.
|
||||
* @param PriceDTO $dto
|
||||
* @param Pricedetail $entity
|
||||
* @return Pricedetail
|
||||
*/
|
||||
public function convertPrice(PriceDTO $dto, Pricedetail $entity = new Pricedetail()): Pricedetail
|
||||
{
|
||||
$entity->setMinDiscountQuantity($dto->minimum_discount_amount);
|
||||
|
@ -84,6 +98,9 @@ class DTOtoEntityConverter
|
|||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given DTO to an orderdetail entity.
|
||||
*/
|
||||
public function convertPurchaseInfo(PurchaseInfoDTO $dto, Orderdetail $entity = new Orderdetail()): Orderdetail
|
||||
{
|
||||
$entity->setSupplierpartnr($dto->order_number);
|
||||
|
@ -97,10 +114,19 @@ class DTOtoEntityConverter
|
|||
return $entity;
|
||||
}
|
||||
|
||||
public function convertFile(FileDTO $dto, PartAttachment $entity = new PartAttachment()): PartAttachment
|
||||
/**
|
||||
* Converts the given DTO to an Attachment entity.
|
||||
* @param FileDTO $dto
|
||||
* @param AttachmentType $type The type which should be used for the attachment
|
||||
* @param PartAttachment $entity
|
||||
* @return PartAttachment
|
||||
*/
|
||||
public function convertFile(FileDTO $dto, AttachmentType $type, PartAttachment $entity = new PartAttachment()): PartAttachment
|
||||
{
|
||||
$entity->setURL($dto->url);
|
||||
|
||||
$entity->setAttachmentType($type);
|
||||
|
||||
//If no name is given, try to extract the name from the URL
|
||||
if (empty($dto->name)) {
|
||||
$entity->setName(basename($dto->url));
|
||||
|
@ -137,8 +163,9 @@ class DTOtoEntityConverter
|
|||
}
|
||||
|
||||
//Add datasheets
|
||||
$datasheet_type = $this->getDatasheetType();
|
||||
foreach ($dto->datasheets ?? [] as $datasheet) {
|
||||
$entity->addAttachment($this->convertFile($datasheet));
|
||||
$entity->addAttachment($this->convertFile($datasheet, $datasheet_type));
|
||||
}
|
||||
|
||||
//Add orderdetails and prices
|
||||
|
@ -150,6 +177,8 @@ class DTOtoEntityConverter
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the existing entity of the given class with the given name or create it if it does not exist.
|
||||
* If the name is null, null is returned.
|
||||
* @template T of AbstractStructuralDBElement
|
||||
* @param string $class
|
||||
* @phpstan-param class-string<T> $class
|
||||
|
@ -168,6 +197,7 @@ class DTOtoEntityConverter
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the existing entity of the given class with the given name or create it if it does not exist.
|
||||
* @template T of AbstractStructuralDBElement
|
||||
* @param string $class The class of the entity to create
|
||||
* @phpstan-param class-string<T> $class
|
||||
|
@ -180,6 +210,11 @@ class DTOtoEntityConverter
|
|||
return $this->em->getRepository($class)->findOrCreateForInfoProvider($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the currency entity for the given ISO code or create it if it does not exist
|
||||
* @param string $iso_code
|
||||
* @return Currency|null
|
||||
*/
|
||||
private function getCurrency(string $iso_code): ?Currency
|
||||
{
|
||||
//Check if the currency is the base currency (then we can just return null)
|
||||
|
@ -190,4 +225,38 @@ class DTOtoEntityConverter
|
|||
return $this->em->getRepository(Currency::class)->findOrCreateByISOCode($iso_code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attachment type used for datasheets or creates it if it does not exist
|
||||
* @return AttachmentType
|
||||
*/
|
||||
private function getDatasheetType(): AttachmentType
|
||||
{
|
||||
/** @var AttachmentType $tmp */
|
||||
$tmp = $this->em->getRepository(AttachmentType::class)->findOrCreateForInfoProvider(self::TYPE_DATASHEETS_NAME);
|
||||
|
||||
//If the entity was newly created, set the file filter
|
||||
if ($tmp->getId() === null) {
|
||||
$tmp->setFiletypeFilter('application/pdf');
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the attachment type used for datasheets or creates it if it does not exist
|
||||
* @return AttachmentType
|
||||
*/
|
||||
private function getImageType(): AttachmentType
|
||||
{
|
||||
/** @var AttachmentType $tmp */
|
||||
$tmp = $this->em->getRepository(AttachmentType::class)->findOrCreateForInfoProvider(self::TYPE_IMAGE_NAME);
|
||||
|
||||
//If the entity was newly created, set the file filter
|
||||
if ($tmp->getId() === null) {
|
||||
$tmp->setFiletypeFilter('image/*');
|
||||
}
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,24 +25,24 @@ namespace App\Services\InfoProviderSystem;
|
|||
|
||||
use App\Services\InfoProviderSystem\Providers\InfoProviderInterface;
|
||||
|
||||
class ProviderRegistry
|
||||
/**
|
||||
* This class keeps track of all registered info providers and allows to find them by their key
|
||||
*/
|
||||
final class ProviderRegistry
|
||||
{
|
||||
|
||||
/**
|
||||
* @var InfoProviderInterface[] The info providers index by their keys
|
||||
* @psalm-var array
|
||||
* @phpstan-var array<string, InfoProviderInterface>
|
||||
*/
|
||||
private array $providers_by_name = [];
|
||||
|
||||
/**
|
||||
* @var InfoProviderInterface[] The enabled providers indexed by their keys
|
||||
* @psalm-var array
|
||||
*/
|
||||
private array $providers_active = [];
|
||||
|
||||
/**
|
||||
* @var InfoProviderInterface[] The disabled providers indexed by their keys
|
||||
* @psalm-var array
|
||||
*/
|
||||
private array $providers_disabled = [];
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue