Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| IdGeneratorAuftrag | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
462 | |
0.00% |
0 / 1 |
| generateId | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
462 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Satag\AmicronEntityBundle\Doctrine; |
| 6 | |
| 7 | use Doctrine\DBAL\Exception; |
| 8 | use Doctrine\ORM\EntityManagerInterface; |
| 9 | use Doctrine\ORM\Id\AbstractIdGenerator; |
| 10 | use Satag\AmicronEntityBundle\Entity\Auftrag; |
| 11 | use Satag\AmicronEntityBundle\Entity\Auftragkunde; |
| 12 | use Satag\AmicronEntityBundle\Entity\Auftraglieferant; |
| 13 | |
| 14 | class IdGeneratorAuftrag extends AbstractIdGenerator |
| 15 | { |
| 16 | /** |
| 17 | * @throws Exception |
| 18 | */ |
| 19 | public function generateId(EntityManagerInterface $em, ?object $entity): mixed |
| 20 | { |
| 21 | if (!$entity instanceof Auftrag) { |
| 22 | throw new \LogicException('Entity must be an instance of Auftrag'); |
| 23 | } |
| 24 | |
| 25 | $auftragArt = $entity->getAuftragart(); |
| 26 | $sequencePart = match ($auftragArt) { |
| 27 | 1 => $entity->getArt() === 'A' ? 'AN' : 'BA', |
| 28 | 2 => $entity->getArt() === 'A' ? 'AB' : 'BE', |
| 29 | 3 => $entity->getArt() === 'A' ? 'LS' : 'WE', |
| 30 | 4 => $entity->getArt() === 'A' ? 'RG' : 'ST', |
| 31 | 5 => $entity->getArt() === 'A' ? 'RB' : 'SO', |
| 32 | 6 => $entity->getArt() === 'A' ? 'GS' : 'ER', |
| 33 | /** @phpstan-ignore match.alwaysFalse */ |
| 34 | Auftragkunde::RETOURENWARENEINGANG, Auftraglieferant::RETOURLIEFERSCHEIN => $entity->getArt() === 'A' ? 'RW' : 'RL', |
| 35 | Auftragkunde::PRODUKTIONRESERVIEREN => 'PR', |
| 36 | Auftragkunde::PRODUKTIONBUCHEN => 'PB', |
| 37 | default => throw new \LogicException(\sprintf('No generator defined for Auftragsart %s', (string) $auftragArt)), |
| 38 | }; |
| 39 | |
| 40 | if (is_a($entity, Auftragkunde::class)) { |
| 41 | $sequenceName = 'GEN_ATR' . $sequencePart . 'NR'; |
| 42 | } else { |
| 43 | $sequenceName = 'GEN_BST' . $sequencePart . 'NR'; |
| 44 | } |
| 45 | |
| 46 | $dbConnection = $em->getConnection(); |
| 47 | $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL($sequenceName); |
| 48 | $nextNr = $dbConnection->fetchOne($nextvalQuery); |
| 49 | |
| 50 | if (!\is_scalar($nextNr)) { |
| 51 | throw new \LogicException('Error in sequence ' . $sequenceName); |
| 52 | } |
| 53 | |
| 54 | $entity->setAuftragnr((string) $nextNr); |
| 55 | |
| 56 | $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('GEN_LFDNR'); |
| 57 | |
| 58 | return $dbConnection->fetchOne($nextvalQuery); |
| 59 | } |
| 60 | } |