Part-DB.Part-DB-server/src/Services/LabelSystem/Barcodes/BarcodeExampleElementsGenerator.php

142 lines
4.7 KiB
PHP
Raw Normal View History

<?php
2022-11-29 21:21:26 +01:00
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 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/>.
*/
2020-05-10 21:39:31 +02:00
declare(strict_types=1);
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 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/>.
*/
2020-05-09 18:17:23 +02:00
namespace App\Services\LabelSystem\Barcodes;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Entity\Parts\Category;
use App\Entity\Parts\Footprint;
use App\Entity\Parts\Manufacturer;
use App\Entity\Parts\Part;
use App\Entity\Parts\PartLot;
use App\Entity\Parts\Storelocation;
2022-08-14 19:32:53 +02:00
use DateTime;
use InvalidArgumentException;
use ReflectionClass;
2020-05-09 18:17:23 +02:00
final class BarcodeExampleElementsGenerator
{
public function getElement(string $type): object
{
switch ($type) {
case 'part':
return $this->getExamplePart();
case 'part_lot':
return $this->getExamplePartLot();
case 'storelocation':
return $this->getStorelocation();
default:
2022-08-14 19:32:53 +02:00
throw new InvalidArgumentException('Unknown $type.');
}
}
public function getExamplePart(): Part
{
$part = new Part();
$part->setName('Example Part');
$part->setDescription('<b>Part</b> description');
$part->setComment('<i>Part</i> comment');
$part->setCategory($this->getStructuralData(Category::class));
$part->setFootprint($this->getStructuralData(Footprint::class));
$part->setManufacturer($this->getStructuralData(Manufacturer::class));
$part->setMass(123.4);
$part->setManufacturerProductNumber('CUSTOM MPN');
$part->setTags('Tag1, Tag2, Tag3');
$part->setManufacturingStatus('active');
$part->updateTimestamps();
$part->setFavorite(true);
$part->setMinAmount(100);
$part->setNeedsReview(true);
return $part;
}
public function getExamplePartLot(): PartLot
{
$lot = new PartLot();
$lot->setPart($this->getExamplePart());
$lot->setDescription('Example Lot');
$lot->setComment('Lot comment');
2022-08-14 19:32:53 +02:00
$lot->setExpirationDate(new DateTime('+1 days'));
$lot->setStorageLocation($this->getStructuralData(Storelocation::class));
$lot->setAmount(123);
return $lot;
}
2020-05-10 21:39:31 +02:00
private function getStorelocation(): Storelocation
{
$storelocation = new Storelocation();
$storelocation->setName('Location 1');
$storelocation->setComment('Example comment');
$storelocation->updateTimestamps();
2020-05-10 21:39:31 +02:00
$parent = new Storelocation();
$parent->setName('Parent');
$storelocation->setParent($parent);
return $storelocation;
}
private function getStructuralData(string $class): AbstractStructuralDBElement
{
2020-08-21 21:36:22 +02:00
if (!is_a($class, AbstractStructuralDBElement::class, true)) {
2022-08-14 19:32:53 +02:00
throw new InvalidArgumentException('$class must be an child of AbstractStructuralDBElement');
2020-05-10 21:39:31 +02:00
}
/** @var AbstractStructuralDBElement $parent */
$parent = new $class();
$parent->setName('Example');
/** @var AbstractStructuralDBElement $child */
$child = new $class();
2022-08-14 19:32:53 +02:00
$child->setName((new ReflectionClass($class))->getShortName());
2020-05-10 21:39:31 +02:00
$child->setParent($parent);
return $child;
}
}