Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
IdGeneratorArtikel
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
0.00% covered (danger)
0.00%
0 / 1
 generateId
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
1<?php
2
3declare(strict_types=1);
4
5namespace Satag\AmicronEntityBundle\Doctrine;
6
7use Doctrine\DBAL\Exception;
8use Doctrine\ORM\EntityManagerInterface;
9use Doctrine\ORM\Id\AbstractIdGenerator;
10use Satag\AmicronEntityBundle\Entity\Artikel;
11
12/**
13 * @see \Satag\AmicronEntityBundle\Tests\Doctrine\IdGeneratorArtikelTest
14 */
15class IdGeneratorArtikel extends AbstractIdGenerator
16{
17    /**
18     * @throws Exception
19     */
20    public function generateId(EntityManagerInterface $em, ?object $entity): mixed
21    {
22        if (!$entity instanceof Artikel) {
23            throw new \LogicException('Entity must be an instance of Artikel');
24        }
25
26        $dbConnection = $em->getConnection();
27
28        if ($entity->getArtikelnr() === '') {
29            $sequenceNextValSQL = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('GEN_ARTNR');
30            $nextNr = (string) $dbConnection->fetchOne($sequenceNextValSQL);
31            $entity->setArtikelnr($nextNr);
32        }
33
34        $sequenceNextValSQL = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('GEN_LFDNR');
35
36        return $dbConnection->fetchOne($sequenceNextValSQL);
37    }
38}