mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-01-27 12:39:35 +00:00
Various small code quality improvements
This commit is contained in:
parent
95701a6438
commit
9a654797ef
1 changed files with 14 additions and 21 deletions
|
|
@ -89,7 +89,6 @@ use App\Services\InfoProviderSystem\DTOs\PriceDTO;
|
||||||
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
use App\Services\InfoProviderSystem\DTOs\PurchaseInfoDTO;
|
||||||
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
|
use App\Services\InfoProviderSystem\DTOs\ParameterDTO;
|
||||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||||
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
||||||
use Psr\Cache\CacheItemPoolInterface;
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -98,8 +97,6 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
|
|
||||||
private const ENDPOINT_URL = 'https://oemsecretsapi.com/partsearch';
|
private const ENDPOINT_URL = 'https://oemsecretsapi.com/partsearch';
|
||||||
|
|
||||||
public const DISTRIBUTOR_NAME = 'OEMSecrets';
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly HttpClientInterface $oemsecretsClient,
|
private readonly HttpClientInterface $oemsecretsClient,
|
||||||
private readonly string $api_key,
|
private readonly string $api_key,
|
||||||
|
|
@ -426,7 +423,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
$details = $cacheItem->get();
|
$details = $cacheItem->get();
|
||||||
} else {
|
} else {
|
||||||
// If the details are not found in the cache, throw an exception
|
// If the details are not found in the cache, throw an exception
|
||||||
throw new \Exception("Details not found for provider_id $id");
|
throw new \RuntimeException("Details not found for provider_id $id");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $details;
|
return $details;
|
||||||
|
|
@ -493,12 +490,9 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
): ?PartDetailDTO
|
): ?PartDetailDTO
|
||||||
{
|
{
|
||||||
if (!isset($product['manufacturer'], $product['part_number'])) {
|
if (!isset($product['manufacturer'], $product['part_number'])) {
|
||||||
throw new \Exception("Missing required product data: 'manufacturer' or 'part_number'");
|
throw new \InvalidArgumentException("Missing required product data: 'manufacturer' or 'part_number'");
|
||||||
}
|
}
|
||||||
|
|
||||||
$manufacturer = $product['manufacturer'];
|
|
||||||
$part_number = $product['part_number'];
|
|
||||||
|
|
||||||
// Retrieve the country_code associated with the distributor and store it in the $distributorCountryCodes array.
|
// Retrieve the country_code associated with the distributor and store it in the $distributorCountryCodes array.
|
||||||
$distributorCountry = $product['distributor']['distributor_country'] ?? null;
|
$distributorCountry = $product['distributor']['distributor_country'] ?? null;
|
||||||
$distributorName = $product['distributor']['distributor_name'] ?? null;
|
$distributorName = $product['distributor']['distributor_name'] ?? null;
|
||||||
|
|
@ -635,12 +629,12 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
preview_image_url: $basicInfoResults[$provider_id]['preview_image_url'],
|
preview_image_url: $basicInfoResults[$provider_id]['preview_image_url'],
|
||||||
manufacturing_status: $basicInfoResults[$provider_id]['manufacturing_status'],
|
manufacturing_status: $basicInfoResults[$provider_id]['manufacturing_status'],
|
||||||
provider_url: $basicInfoResults[$provider_id]['provider_url'],
|
provider_url: $basicInfoResults[$provider_id]['provider_url'],
|
||||||
vendor_infos: $purchaseInfoResults[$provider_id] ?? [],
|
footprint: $basicInfoResults[$provider_id]['footprint'] ?? null,
|
||||||
|
notes: $basicInfoResults[$provider_id]['notes'] ?? null,
|
||||||
datasheets: $datasheetsResults[$provider_id] ?? [],
|
datasheets: $datasheetsResults[$provider_id] ?? [],
|
||||||
images: $imagesResults[$provider_id] ?? [],
|
images: $imagesResults[$provider_id] ?? [],
|
||||||
parameters: $parametersResults[$provider_id] ?? [],
|
parameters: $parametersResults[$provider_id] ?? [],
|
||||||
notes: $basicInfoResults[$provider_id]['notes'] ?? null,
|
vendor_infos: $purchaseInfoResults[$provider_id] ?? []
|
||||||
footprint: $basicInfoResults[$provider_id]['footprint'] ?? null
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Force garbage collection to deallocate unused memory cycles
|
// Force garbage collection to deallocate unused memory cycles
|
||||||
|
|
@ -997,7 +991,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
} else {
|
} else {
|
||||||
// If "event_link" does not exist, try to extract the name from the main URL path
|
// If "event_link" does not exist, try to extract the name from the main URL path
|
||||||
$sheetName = basename($urlComponents['path']);
|
$sheetName = basename($urlComponents['path']);
|
||||||
if (strpos($sheetName, '.') === false || !preg_match('/\.(pdf|doc|docx|xls|xlsx|ppt|pptx)$/i', $sheetName)) {
|
if (!str_contains($sheetName, '.') || !preg_match('/\.(pdf|doc|docx|xls|xlsx|ppt|pptx)$/i', $sheetName)) {
|
||||||
// If the name does not have a valid extension, assign a default name
|
// If the name does not have a valid extension, assign a default name
|
||||||
$sheetName = 'datasheet_' . uniqid('', true) . '.pdf';
|
$sheetName = 'datasheet_' . uniqid('', true) . '.pdf';
|
||||||
}
|
}
|
||||||
|
|
@ -1005,7 +999,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create an array of existing file names
|
// Create an array of existing file names
|
||||||
$existingNames = array_map(function ($existingDatasheet) {
|
$existingNames = array_map(static function ($existingDatasheet) {
|
||||||
return $existingDatasheet->name;
|
return $existingDatasheet->name;
|
||||||
}, $existingDatasheets);
|
}, $existingDatasheets);
|
||||||
|
|
||||||
|
|
@ -1097,7 +1091,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
$part = trim($part);
|
$part = trim($part);
|
||||||
|
|
||||||
// Check if the part contains a key-value structure
|
// Check if the part contains a key-value structure
|
||||||
if (strpos($part, ':') !== false) {
|
if (str_contains($part, ':')) {
|
||||||
[$name, $value] = explode(':', $part, 2);
|
[$name, $value] = explode(':', $part, 2);
|
||||||
$name = trim($name);
|
$name = trim($name);
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
|
|
@ -1114,10 +1108,10 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
|
|
||||||
$parameters[] = new ParameterDTO(
|
$parameters[] = new ParameterDTO(
|
||||||
name: $parsedValue['name'],
|
name: $parsedValue['name'],
|
||||||
|
value_text: $parsedValue['value_text'] ?? null,
|
||||||
value_typ: $value_typ,
|
value_typ: $value_typ,
|
||||||
value_min: $value_min,
|
value_min: $value_min,
|
||||||
value_max: $value_max,
|
value_max: $value_max,
|
||||||
value_text: $parsedValue['value_text'] ?? null,
|
|
||||||
unit: $parsedValue['unit'] ?? null, // Add extracted unit
|
unit: $parsedValue['unit'] ?? null, // Add extracted unit
|
||||||
symbol: $parsedValue['symbol'] ?? null // Add extracted symbol
|
symbol: $parsedValue['symbol'] ?? null // Add extracted symbol
|
||||||
);
|
);
|
||||||
|
|
@ -1169,7 +1163,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
$value = trim($value);
|
$value = trim($value);
|
||||||
|
|
||||||
// Handle ranges and plus/minus signs
|
// Handle ranges and plus/minus signs
|
||||||
if (strpos($value, '...') !== false || strpos($value, '~') !== false || strpos($value, '±') !== false) {
|
if (str_contains($value, '...') || str_contains($value, '~') || str_contains($value, '±')) {
|
||||||
// Handle ranges
|
// Handle ranges
|
||||||
$value = str_replace(['...', '~'], '...', $value); // Normalize range separators
|
$value = str_replace(['...', '~'], '...', $value); // Normalize range separators
|
||||||
$rangeParts = preg_split('/\s*[\.\~]\s*/', $value);
|
$rangeParts = preg_split('/\s*[\.\~]\s*/', $value);
|
||||||
|
|
@ -1187,7 +1181,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
$result['unit'] = $parsedMax['unit'] ?? $parsedMin['unit'];
|
$result['unit'] = $parsedMax['unit'] ?? $parsedMin['unit'];
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif (strpos($value, '@') !== false) {
|
} elseif (str_contains($value, '@')) {
|
||||||
// If we find "@", we treat it as additional textual information
|
// If we find "@", we treat it as additional textual information
|
||||||
[$numericValue, $textValue] = explode('@', $value);
|
[$numericValue, $textValue] = explode('@', $value);
|
||||||
$result['value_typ'] = (float) $numericValue;
|
$result['value_typ'] = (float) $numericValue;
|
||||||
|
|
@ -1274,8 +1268,7 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
$baseUrl = rtrim($this->getProviderInfo()['url'], '/') . '/';
|
$baseUrl = rtrim($this->getProviderInfo()['url'], '/') . '/';
|
||||||
$inquiryPath = trim($oemInquiry, '/') . '/';
|
$inquiryPath = trim($oemInquiry, '/') . '/';
|
||||||
$encodedPartNumber = urlencode(trim($partNumber));
|
$encodedPartNumber = urlencode(trim($partNumber));
|
||||||
$inquiryUrl = $baseUrl . $inquiryPath . $encodedPartNumber;
|
return $baseUrl . $inquiryPath . $encodedPartNumber;
|
||||||
return $inquiryUrl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -1363,8 +1356,8 @@ class OEMSecretsProvider implements InfoProviderInterface
|
||||||
private function compareByCompleteness(object $a, object $b): int
|
private function compareByCompleteness(object $a, object $b): int
|
||||||
{
|
{
|
||||||
// Calculate the completeness score for each object
|
// Calculate the completeness score for each object
|
||||||
$completenessA = (int)$this->calculateCompleteness($a);
|
$completenessA = $this->calculateCompleteness($a);
|
||||||
$completenessB = (int)$this->calculateCompleteness($b);
|
$completenessB = $this->calculateCompleteness($b);
|
||||||
|
|
||||||
// Sort in descending order by completeness (higher score is better)
|
// Sort in descending order by completeness (higher score is better)
|
||||||
return $completenessB - $completenessA;
|
return $completenessB - $completenessA;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue