Use symfony native functions to generate the routes for part creation

This commit is contained in:
Jan Böhmer 2026-02-21 22:43:42 +01:00
parent 0010ee8de1
commit 5100469751

View file

@ -42,8 +42,10 @@ declare(strict_types=1);
namespace App\Controller; namespace App\Controller;
use App\Form\LabelSystem\ScanDialogType; use App\Form\LabelSystem\ScanDialogType;
use App\Services\InfoProviderSystem\Providers\LCSCProvider;
use App\Services\LabelSystem\BarcodeScanner\BarcodeRedirector; use App\Services\LabelSystem\BarcodeScanner\BarcodeRedirector;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanHelper; use App\Services\LabelSystem\BarcodeScanner\BarcodeScanHelper;
use App\Services\LabelSystem\BarcodeScanner\BarcodeScanResultInterface;
use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType; use App\Services\LabelSystem\BarcodeScanner\BarcodeSourceType;
use App\Services\LabelSystem\BarcodeScanner\LocalBarcodeScanResult; use App\Services\LabelSystem\BarcodeScanner\LocalBarcodeScanResult;
use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult; use App\Services\LabelSystem\BarcodeScanner\LCSCBarcodeScanResult;
@ -105,7 +107,7 @@ class ScanController extends AbstractController
return $this->redirect($url); return $this->redirect($url);
} catch (EntityNotFoundException) { } catch (EntityNotFoundException) {
// Decoded OK, but no part is found. If its a vendor code, redirect to create. // Decoded OK, but no part is found. If its a vendor code, redirect to create.
$createUrl = $this->buildCreateUrlForScanResult($scan, $request->getLocale()); $createUrl = $this->buildCreateUrlForScanResult($scan);
if ($createUrl !== null) { if ($createUrl !== null) {
return $this->redirect($createUrl); return $this->redirect($createUrl);
} }
@ -161,21 +163,19 @@ class ScanController extends AbstractController
/** /**
* Builds a URL for creating a new part based on the barcode data * Builds a URL for creating a new part based on the barcode data
* @param object $scanResult * @param BarcodeScanResultInterface $scanResult
* @param string $locale
* @return string|null * @return string|null
*/ */
private function buildCreateUrlForScanResult(object $scanResult, string $locale): ?string private function buildCreateUrlForScanResult(BarcodeScanResultInterface $scanResult): ?string
{ {
// LCSC // LCSC
if ($scanResult instanceof LCSCBarcodeScanResult) { if ($scanResult instanceof LCSCBarcodeScanResult) {
$lcscCode = $scanResult->getPC(); $lcscCode = $scanResult->getPC();
if (is_string($lcscCode) && $lcscCode !== '') { if ($lcscCode !== null && $lcscCode !== '') {
return '/' return $this->generateUrl('info_providers_create_part', [
. rawurlencode($locale) 'providerKey' => 'lcsc',
. '/part/from_info_provider/lcsc/' 'providerId' => $lcscCode,
. rawurlencode($lcscCode) ]);
. '/create';
} }
} }
@ -185,7 +185,7 @@ class ScanController extends AbstractController
// Mouser: use supplierPartNumber -> search provider -> provider_id // Mouser: use supplierPartNumber -> search provider -> provider_id
if ($vendor === 'mouser' if ($vendor === 'mouser'
&& is_string($scanResult->supplierPartNumber) && $scanResult->supplierPartNumber !== null
&& $scanResult->supplierPartNumber !== '' && $scanResult->supplierPartNumber !== ''
) { ) {
try { try {
@ -204,12 +204,12 @@ class ScanController extends AbstractController
// If there are results, provider_id is MouserPartNumber (per MouserProvider.php) // If there are results, provider_id is MouserPartNumber (per MouserProvider.php)
$best = $dtos[0] ?? null; $best = $dtos[0] ?? null;
if ($best !== null && is_string($best->provider_id) && $best->provider_id !== '') { if ($best !== null && $best->provider_id !== '') {
return '/'
. rawurlencode($locale) return $this->generateUrl('info_providers_create_part', [
. '/part/from_info_provider/mouser/' 'providerKey' => 'mouser',
. rawurlencode($best->provider_id) 'providerId' => $best->provider_id,
. '/create'; ]);
} }
$this->addFlash('warning', 'No Mouser match found for this MPN.'); $this->addFlash('warning', 'No Mouser match found for this MPN.');
@ -238,11 +238,10 @@ class ScanController extends AbstractController
$id = $scanResult->customerPartNumber ?: $scanResult->supplierPartNumber; $id = $scanResult->customerPartNumber ?: $scanResult->supplierPartNumber;
if (is_string($id) && $id !== '') { if (is_string($id) && $id !== '') {
return '/' return $this->generateUrl('info_providers_create_part', [
. rawurlencode($locale) 'providerKey' => 'digikey',
. '/part/from_info_provider/digikey/' 'providerId' => $id,
. rawurlencode($id) ]);
. '/create';
} }
} catch (\InvalidArgumentException) { } catch (\InvalidArgumentException) {
$this->addFlash('warning', 'Digi-Key provider is not installed/enabled'); $this->addFlash('warning', 'Digi-Key provider is not installed/enabled');
@ -312,7 +311,6 @@ class ScanController extends AbstractController
$input = trim((string) $request->request->get('input', '')); $input = trim((string) $request->request->get('input', ''));
$mode = (string) ($request->request->get('mode') ?? ''); $mode = (string) ($request->request->get('mode') ?? '');
$infoMode = (bool) filter_var($request->request->get('info_mode', false), FILTER_VALIDATE_BOOL); $infoMode = (bool) filter_var($request->request->get('info_mode', false), FILTER_VALIDATE_BOOL);
$locale = $request->getLocale();
if ($input === '') { if ($input === '') {
return new JsonResponse(['ok' => false], 200); return new JsonResponse(['ok' => false], 200);
@ -364,7 +362,7 @@ class ScanController extends AbstractController
// Create link only when NOT found (vendor codes) // Create link only when NOT found (vendor codes)
$createUrl = null; $createUrl = null;
if (!$targetFound) { if (!$targetFound) {
$createUrl = $this->buildCreateUrlForScanResult($scan, $locale); $createUrl = $this->buildCreateUrlForScanResult($scan);
} }
// Render fragment (use openUrl for universal "Open" link) // Render fragment (use openUrl for universal "Open" link)