mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-26 19:58:53 +02:00
Refactored barcode scan functions
This is preparatory work for issue #373
This commit is contained in:
parent
563edb1731
commit
bdcd51d533
8 changed files with 322 additions and 162 deletions
|
@ -41,6 +41,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
use App\Entity\LabelSystem\LabelSupportedElement;
|
||||
use App\Entity\Parts\PartLot;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityNotFoundException;
|
||||
|
@ -59,32 +60,30 @@ final class BarcodeRedirector
|
|||
/**
|
||||
* Determines the URL to which the user should be redirected, when scanning a QR code.
|
||||
*
|
||||
* @param string $type The type of the element that was scanned (e.g. 'part', 'lot', etc.)
|
||||
* @param int $id The ID of the element that was scanned
|
||||
*
|
||||
* @param BarcodeScanResult $barcodeScan The result of the barcode scan
|
||||
* @return string the URL to which should be redirected
|
||||
*
|
||||
* @throws EntityNotFoundException
|
||||
*/
|
||||
public function getRedirectURL(string $type, int $id): string
|
||||
public function getRedirectURL(BarcodeScanResult $barcodeScan): string
|
||||
{
|
||||
switch ($type) {
|
||||
case 'part':
|
||||
return $this->urlGenerator->generate('app_part_show', ['id' => $id]);
|
||||
case 'lot':
|
||||
switch ($barcodeScan->target_type) {
|
||||
case LabelSupportedElement::PART:
|
||||
return $this->urlGenerator->generate('app_part_show', ['id' => $barcodeScan->target_id]);
|
||||
case LabelSupportedElement::PART_LOT:
|
||||
//Try to determine the part to the given lot
|
||||
$lot = $this->em->find(PartLot::class, $id);
|
||||
$lot = $this->em->find(PartLot::class, $barcodeScan->target_id);
|
||||
if (!$lot instanceof PartLot) {
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
|
||||
return $this->urlGenerator->generate('app_part_show', ['id' => $lot->getPart()->getID()]);
|
||||
|
||||
case 'location':
|
||||
return $this->urlGenerator->generate('part_list_store_location', ['id' => $id]);
|
||||
case LabelSupportedElement::STORELOCATION:
|
||||
return $this->urlGenerator->generate('part_list_store_location', ['id' => $barcodeScan->target_id]);
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException('Unknown $type: '.$type);
|
||||
throw new InvalidArgumentException('Unknown $type: '.$barcodeScan->target_type->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,24 +41,59 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
use App\Entity\LabelSystem\LabelSupportedElement;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @see \App\Tests\Services\LabelSystem\Barcodes\BarcodeNormalizerTest
|
||||
*/
|
||||
final class BarcodeNormalizer
|
||||
final class BarcodeScanHelper
|
||||
{
|
||||
private const PREFIX_TYPE_MAP = [
|
||||
'L' => 'lot',
|
||||
'P' => 'part',
|
||||
'S' => 'location',
|
||||
'L' => LabelSupportedElement::PART_LOT,
|
||||
'P' => LabelSupportedElement::PART,
|
||||
'S' => LabelSupportedElement::STORELOCATION,
|
||||
];
|
||||
|
||||
public const QR_TYPE_MAP = [
|
||||
'lot' => LabelSupportedElement::PART_LOT,
|
||||
'part' => LabelSupportedElement::PART,
|
||||
'location' => LabelSupportedElement::STORELOCATION,
|
||||
];
|
||||
|
||||
/**
|
||||
* Parses barcode content and normalizes it.
|
||||
* Returns an array in the format ['part', 1]: First entry contains element type, second the ID of the element.
|
||||
* Parse the given barcode content and return the target type and ID.
|
||||
* If the barcode could not be parsed, an exception is thrown.
|
||||
* Using the $type parameter, you can specify how the barcode should be parsed. If set to null, the function
|
||||
* will try to guess the type.
|
||||
* @param string $input
|
||||
* @param BarcodeSourceType|null $type
|
||||
* @return BarcodeScanResult
|
||||
*/
|
||||
public function normalizeBarcodeContent(string $input): array
|
||||
public function scanBarcodeContent(string $input, ?BarcodeSourceType $type = null): BarcodeScanResult
|
||||
{
|
||||
//Do specific parsing
|
||||
if ($type === BarcodeSourceType::INTERNAL) {
|
||||
return $this->parseInternalBarcode($input) ?? throw new InvalidArgumentException('Could not parse barcode');
|
||||
}
|
||||
|
||||
//Null means auto and we try the different formats
|
||||
$result = $this->parseInternalBarcode($input);
|
||||
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
throw new InvalidArgumentException('Unknown barcode format');
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tries to interpret the given barcode content as an internal barcode.
|
||||
* If the barcode could not be parsed at all, null is returned. If the barcode is a valid format, but could
|
||||
* not be found in the database, an exception is thrown.
|
||||
* @param string $input
|
||||
* @return BarcodeScanResult|null
|
||||
*/
|
||||
private function parseInternalBarcode(string $input): ?BarcodeScanResult
|
||||
{
|
||||
$input = trim($input);
|
||||
$matches = [];
|
||||
|
@ -68,7 +103,11 @@ final class BarcodeNormalizer
|
|||
|
||||
//Extract parts from QR code's URL
|
||||
if (preg_match('#^https?://.*/scan/(\w+)/(\d+)/?$#', $input, $matches)) {
|
||||
return [$matches[1], (int) $matches[2]];
|
||||
return new BarcodeScanResult(
|
||||
target_type: self::QR_TYPE_MAP[strtolower($matches[1])],
|
||||
target_id: (int) $matches[2],
|
||||
source_type: BarcodeSourceType::INTERNAL
|
||||
);
|
||||
}
|
||||
|
||||
//New Code39 barcode use L0001 format
|
||||
|
@ -80,7 +119,11 @@ final class BarcodeNormalizer
|
|||
throw new InvalidArgumentException('Unknown prefix '.$prefix);
|
||||
}
|
||||
|
||||
return [self::PREFIX_TYPE_MAP[$prefix], $id];
|
||||
return new BarcodeScanResult(
|
||||
target_type: self::PREFIX_TYPE_MAP[$prefix],
|
||||
target_id: $id,
|
||||
source_type: BarcodeSourceType::INTERNAL
|
||||
);
|
||||
}
|
||||
|
||||
//During development the L-000001 format was used
|
||||
|
@ -92,19 +135,32 @@ final class BarcodeNormalizer
|
|||
throw new InvalidArgumentException('Unknown prefix '.$prefix);
|
||||
}
|
||||
|
||||
return [self::PREFIX_TYPE_MAP[$prefix], $id];
|
||||
return new BarcodeScanResult(
|
||||
target_type: self::PREFIX_TYPE_MAP[$prefix],
|
||||
target_id: $id,
|
||||
source_type: BarcodeSourceType::INTERNAL
|
||||
);
|
||||
}
|
||||
|
||||
//Legacy Part-DB location labels used $L00336 format
|
||||
if (preg_match('#^\$L(\d{5,})$#', $input, $matches)) {
|
||||
return ['location', (int) $matches[1]];
|
||||
return new BarcodeScanResult(
|
||||
target_type: LabelSupportedElement::STORELOCATION,
|
||||
target_id: (int) $matches[1],
|
||||
source_type: BarcodeSourceType::INTERNAL
|
||||
);
|
||||
}
|
||||
|
||||
//Legacy Part-DB used EAN8 barcodes for part labels. Format 0000001(2) (note the optional 8th digit => checksum)
|
||||
if (preg_match('#^(\d{7})\d?$#', $input, $matches)) {
|
||||
return ['part', (int) $matches[1]];
|
||||
return new BarcodeScanResult(
|
||||
target_type: LabelSupportedElement::PART,
|
||||
target_id: (int) $matches[1],
|
||||
source_type: BarcodeSourceType::INTERNAL
|
||||
);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unknown barcode format!');
|
||||
//This function abstain from further parsing
|
||||
return null;
|
||||
}
|
||||
}
|
39
src/Services/LabelSystem/Barcodes/BarcodeScanResult.php
Normal file
39
src/Services/LabelSystem/Barcodes/BarcodeScanResult.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
use App\Entity\LabelSystem\LabelSupportedElement;
|
||||
|
||||
/**
|
||||
* This class represents the result of a barcode scan, with the target type and the ID of the element
|
||||
*/
|
||||
class BarcodeScanResult
|
||||
{
|
||||
public function __construct(
|
||||
public readonly LabelSupportedElement $target_type,
|
||||
public readonly int $target_id,
|
||||
public readonly BarcodeSourceType $source_type,
|
||||
) {
|
||||
}
|
||||
}
|
33
src/Services/LabelSystem/Barcodes/BarcodeSourceType.php
Normal file
33
src/Services/LabelSystem/Barcodes/BarcodeSourceType.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Services\LabelSystem\Barcodes;
|
||||
|
||||
/**
|
||||
* This enum represents the different types, where a barcode/QR-code can be generated from
|
||||
*/
|
||||
enum BarcodeSourceType
|
||||
{
|
||||
/** This Barcode was generated using Part-DB internal recommended barcode generator */
|
||||
case INTERNAL;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue