Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
IdGeneratorAdresse
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 generateId
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 generateAdressNummer
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
56
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\Adresse;
11use Satag\AmicronEntityBundle\Entity\Kunde;
12use Satag\AmicronEntityBundle\Entity\Lieferant;
13use Satag\AmicronEntityBundle\Entity\Mitarbeiter;
14
15class IdGeneratorAdresse extends AbstractIdGenerator
16{
17    /**
18     * @throws Exception
19     */
20    public function generateId(EntityManagerInterface $em, ?object $entity): mixed
21    {
22        if (!\is_object($entity)) {
23            throw new \LogicException('No entity provided');
24        }
25
26        if (!is_a($entity, Adresse::class)) {
27            throw new \LogicException('Entity ' . $entity::class . ' not supportet');
28        }
29
30        if ($entity->getNr() === '') {
31            $this->generateAdressNummer($em, $entity);
32        }
33
34        if ($entity->getLfdnr() !== null) {
35            return $entity->getLfdnr();
36        }
37        $dbConnection = $em->getConnection();
38        $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('GEN_LFDNR');
39
40        return $dbConnection->fetchOne($nextvalQuery);
41    }
42
43    private function generateAdressNummer(EntityManagerInterface $em, object $entity): void
44    {
45        switch ($entity::class) {
46            case Kunde::class:
47                if ($entity->isInteressent()) {
48                    $sequenceName = 'GEN_IKUNNR';
49                } else {
50                    $sequenceName = 'GEN_KUNNR';
51                }
52
53                break;
54            case Mitarbeiter::class:
55                $sequenceName = 'GEN_MABNR';
56                break;
57            case Lieferant::class:
58                $sequenceName = 'GEN_LFRNR';
59                break;
60            default:
61                throw new \LogicException(\sprintf('No generator defined for Class %s', $entity::class));
62        }
63
64        $dbConnection = $em->getConnection();
65        $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL($sequenceName);
66        $nextNr = $dbConnection->fetchOne($nextvalQuery);
67
68        if (!\is_scalar($nextNr)) {
69            throw new \LogicException('Error in sequence ' . $sequenceName);
70        }
71
72        $entity->setNr((string) $nextNr);
73    }
74}