Improved typing and formatting of LCSC provider slightly

This commit is contained in:
Jan Böhmer 2024-02-22 22:41:09 +01:00
parent e3e562a154
commit 0bbfaf9893

View file

@ -24,13 +24,10 @@ declare(strict_types=1);
namespace App\Services\InfoProviderSystem\Providers; namespace App\Services\InfoProviderSystem\Providers;
use App\Entity\Parts\ManufacturingStatus;
use App\Form\InfoProviderSystem\ProviderSelectType;
use App\Services\InfoProviderSystem\DTOs\FileDTO; use App\Services\InfoProviderSystem\DTOs\FileDTO;
use App\Services\InfoProviderSystem\DTOs\ParameterDTO; use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
use App\Services\InfoProviderSystem\DTOs\PartDetailDTO; use App\Services\InfoProviderSystem\DTOs\PartDetailDTO;
use App\Services\InfoProviderSystem\DTOs\PriceDTO; use App\Services\InfoProviderSystem\DTOs\PriceDTO;
use App\Services\InfoProviderSystem\DTOs\SearchResultDTO;
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO; use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\HttpClientInterface;
@ -82,14 +79,11 @@ class LCSCProvider implements InfoProviderInterface
$arr = $response->toArray(); $arr = $response->toArray();
$product = $arr['result'] ?? null; $product = $arr['result'] ?? null;
if ($product == null) if ($product === null) {
{
throw new \RuntimeException('Could not find product code: ' . $id); throw new \RuntimeException('Could not find product code: ' . $id);
} }
$dto = $this->getPartDetail($product); return $this->getPartDetail($product);
return $dto;
} }
/** /**
@ -116,7 +110,7 @@ class LCSCProvider implements InfoProviderInterface
// LCSC does not display LCSC codes in the search, instead taking you directly to the // LCSC does not display LCSC codes in the search, instead taking you directly to the
// detailed product listing. It does so utilizing a product tip field. // detailed product listing. It does so utilizing a product tip field.
// If product tip exists and there are no products in the product list try a detail query // If product tip exists and there are no products in the product list try a detail query
if (count($products) == 0 && !($tipProductCode == null)) { if (count($products) === 0 && !($tipProductCode === null)) {
$result[] = $this->queryDetail($tipProductCode); $result[] = $this->queryDetail($tipProductCode);
} }
@ -132,7 +126,7 @@ class LCSCProvider implements InfoProviderInterface
* @param array $product * @param array $product
* @return PartDetailDTO * @return PartDetailDTO
*/ */
function getPartDetail($product): PartDetailDTO private function getPartDetail(array $product): PartDetailDTO
{ {
// Get product images in advance // Get product images in advance
$product_images = $this->getProductImages($product['productImages'] ?? null); $product_images = $this->getProductImages($product['productImages'] ?? null);
@ -145,11 +139,11 @@ class LCSCProvider implements InfoProviderInterface
// LCSC puts HTML in footprints and descriptions sometimes randomly // LCSC puts HTML in footprints and descriptions sometimes randomly
$footprint = $product["encapStandard"] ?? null; $footprint = $product["encapStandard"] ?? null;
if ($footprint != null) { if ($footprint !== null) {
$footprint = strip_tags($footprint); $footprint = strip_tags($footprint);
} }
$part_detail = new PartDetailDTO( return new PartDetailDTO(
provider_key: $this->getProviderKey(), provider_key: $this->getProviderKey(),
provider_id: $product['productCode'], provider_id: $product['productCode'],
name: $product['productModel'], name: $product['productModel'],
@ -159,15 +153,13 @@ class LCSCProvider implements InfoProviderInterface
preview_image_url: $product['productImageUrl'], preview_image_url: $product['productImageUrl'],
manufacturing_status: null, manufacturing_status: null,
provider_url: $this->getProductShortURL($product['productCode']), provider_url: $this->getProductShortURL($product['productCode']),
footprint: $footprint,
datasheets: $this->getProductDatasheets($product['pdfUrl'] ?? null), datasheets: $this->getProductDatasheets($product['pdfUrl'] ?? null),
images: $product_images,
parameters: $this->attributesToParameters($product['paramVOList'] ?? []), parameters: $this->attributesToParameters($product['paramVOList'] ?? []),
vendor_infos: $this->pricesToVendorInfo($product['productCode'], $this->getProductShortURL($product['productCode']), $product['productPriceList'] ?? []), vendor_infos: $this->pricesToVendorInfo($product['productCode'], $this->getProductShortURL($product['productCode']), $product['productPriceList'] ?? []),
footprint: $footprint,
images: $product_images,
mass: $product['weight'] ?? null, mass: $product['weight'] ?? null,
); );
return $part_detail;
} }
/** /**
@ -219,7 +211,7 @@ class LCSCProvider implements InfoProviderInterface
* @param string $product_code * @param string $product_code
* @return string * @return string
*/ */
private function getProductShortURL($product_code): string private function getProductShortURL(string $product_code): string
{ {
return 'https://www.lcsc.com/product-detail/' . $product_code .'.html'; return 'https://www.lcsc.com/product-detail/' . $product_code .'.html';
} }
@ -229,9 +221,9 @@ class LCSCProvider implements InfoProviderInterface
* @param string $url * @param string $url
* @return FileDTO[] * @return FileDTO[]
*/ */
private function getProductDatasheets($url): array private function getProductDatasheets(?string $url): array
{ {
if ($url == null) { if ($url === null) {
return []; return [];
} }
@ -240,17 +232,12 @@ class LCSCProvider implements InfoProviderInterface
/** /**
* Returns a FileDTO array with a list of product images * Returns a FileDTO array with a list of product images
* @param array $images * @param array|null $images
* @return FileDTO[] * @return FileDTO[]
*/ */
private function getProductImages($images): array private function getProductImages(?array $images): array
{ {
$result = []; return array_map(static fn($image) => new FileDTO($image), $images ?? []);
foreach ($images as $image) {
$result[] = new FileDTO($image);
}
return $result;
} }
/** /**
@ -262,7 +249,6 @@ class LCSCProvider implements InfoProviderInterface
$result = []; $result = [];
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
$result[] = ParameterDTO::parseValueField(name: $attribute['paramNameEn'], value: $attribute['paramValueEn'], unit: null, group: null); $result[] = ParameterDTO::parseValueField(name: $attribute['paramNameEn'], value: $attribute['paramValueEn'], unit: null, group: null);
} }