Refactor LoadMotives fixture to support hierarchical motive relationships

- Implemented parent/child relationships in motive construction logic.
- Refactored data parsing to handle nested motives with labels and associated attributes.
- Updated fixtures to align with the new hierarchical motive structure.
This commit is contained in:
2025-09-24 21:06:49 +02:00
parent 433b6c2d71
commit f50b7ee1cd

View File

@@ -13,7 +13,6 @@ namespace Chill\TicketBundle\DataFixtures\ORM;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\TicketBundle\Entity\EmergencyStatusEnum;
use Chill\TicketBundle\Entity\Motive;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
@@ -35,6 +34,7 @@ final class LoadMotives extends Fixture implements FixtureGroupInterface
['label' => '☀️ De 07h à 21h', 'path' => __DIR__.'/docs/peloton_2.pdf'],
['label' => 'Dimanche et jours fériés', 'path' => __DIR__.'/docs/schema_1.png'],
];
$motivesByLabel = [];
foreach (explode("\n", self::MOTIVES) as $row) {
if ('' === trim($row)) {
@@ -46,50 +46,65 @@ final class LoadMotives extends Fixture implements FixtureGroupInterface
continue;
}
$motive = new Motive();
$motive->setLabel(['fr' => trim((string) $data[0])]);
$motive->setMakeTicketEmergency(match ($data[1]) {
'true' => EmergencyStatusEnum::YES,
'false' => EmergencyStatusEnum::NO,
default => throw new \UnexpectedValueException('Unexpected value'),
});
$labels = explode(' > ', $data[0]);
$parent = null;
$numberOfDocs = (int) $data[2];
for ($i = 1; $i <= $numberOfDocs; ++$i) {
$doc = $docs[$i - 1];
$storedObject = new StoredObject();
$storedObject->setTitle($doc['label']);
while (count($labels) > 0) {
$label = array_shift($labels);
dump($labels);
if (isset($motivesByLabel[$label])) {
$motive = $motivesByLabel[$label];
} else {
$motive = new Motive();
$motive->setLabel(['fr' => $label]);
$motivesByLabel[$label] = $motive;
}
$content = file_get_contents($doc['path']);
$contentType = match (substr($doc['path'], -3, 3)) {
'pdf' => 'application/pdf',
'png' => 'image/png',
default => throw new \UnexpectedValueException('Not supported content type here'),
};
$this->storedObjectManager->write($storedObject, $content, $contentType);
if (null !== $parent) {
$motive->setParent($parent);
}
$motive->addStoredObject($storedObject);
$manager->persist($storedObject);
}
$manager->persist($motive);
$parent = $motive;
if (0 === count($labels)) {
// this is the last one, we add data
$numberOfDocs = (int) $data[2];
for ($i = 1; $i <= $numberOfDocs; ++$i) {
$doc = $docs[$i - 1];
$storedObject = new StoredObject();
$storedObject->setTitle($doc['label']);
$content = file_get_contents($doc['path']);
$contentType = match (substr($doc['path'], -3, 3)) {
'pdf' => 'application/pdf',
'png' => 'image/png',
default => throw new \UnexpectedValueException('Not supported content type here'),
};
$this->storedObjectManager->write($storedObject, $content, $contentType);
$motive->addStoredObject($storedObject);
$manager->persist($storedObject);
}
foreach (array_slice($data, 3) as $supplementaryComment) {
if ('' !== trim((string) $supplementaryComment)) {
$motive->addSupplementaryComment(['label' => trim((string) $supplementaryComment)]);
foreach (array_slice($data, 3) as $supplementaryComment) {
if ('' !== trim((string) $supplementaryComment)) {
$motive->addSupplementaryComment(['label' => trim((string) $supplementaryComment)]);
}
}
}
}
$manager->persist($motive);
}
$manager->flush();
}
private const MOTIVES = <<<'CSV'
"Coordonnées",false,"3","Nouvelles coordonnées",
"Horaire de passage",false,"0",
"Retard de livraison",false,"0",
"Erreur de livraison",false,"0",
"Colis incomplet",false,"0",
"Motif administratif > Coordonnées",false,"3","Nouvelles coordonnées",
"Organisation > Horaire de passage",false,"0",
"Organisation > Livraison > Retard de livraison",false,"0",
"Organisation > Livraison > Erreur de livraison",false,"0",
"Organisation > Livraison > Colis incomplet",false,"0",
"MATLOC",false,"0",
"Retard DASRI",false,"1",
"Planning d'astreintes",false,"0",
@@ -116,7 +131,7 @@ final class LoadMotives extends Fixture implements FixtureGroupInterface
"Mauvaise adresse",false,"0",
"Patient absent",false,"0",
"Annulation",false,"0",
"Colis perdu",false,"0",
"Organisation > Livraison > Colis perdu",false,"0",
"Changement de rendez-vous",false,"0",
"Coordination interservices",false,"0",
"Problème de substitution produits",true,"0",