Commit fb3b874e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

[FIX] Correction et amélioration du calcul du nom de fichier du logo (existant...

[FIX] Correction et amélioration du calcul du nom de fichier du logo (existant ou nouveau) d'une structure.
parent cd388888
Loading
Loading
Loading
Loading
+32 −32
Changes for module/Application/tests/ApplicationUnitTest/Service/File/FileServiceTest.php: 32 added lines, 32 removed lines.
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ class FileServiceTest extends \PHPUnit_Framework_TestCase
        $etablissement = $this->createMock(Etablissement::class);
        $etablissement->expects($this->once())->method('getCode')->willReturn('CODE');

        $filename = $this->fileService->computeFileNameForLogoStructure($etablissement);
        $filename = $this->fileService->computeFileNameForNewLogoStructure($etablissement);

        $this->assertEquals('CODE.png', $filename);
    }
@@ -47,7 +47,7 @@ class FileServiceTest extends \PHPUnit_Framework_TestCase
        $etablissement->expects($this->once())->method('getCode')->willReturn(null);
        $etablissement->expects($this->once())->method('generateUniqCode')->willReturn('UNIQID');

        $filename = $this->fileService->computeFileNameForLogoStructure($etablissement);
        $filename = $this->fileService->computeFileNameForNewLogoStructure($etablissement);

        $this->assertEquals('UNIQID.png', $filename);
    }
@@ -59,40 +59,40 @@ class FileServiceTest extends \PHPUnit_Framework_TestCase
        $structure->expects($this->once())->method('getSourceCode')->willReturn('SOURCE_CODE');
        $structure->expects($this->once())->method('getSigle')->willReturn('SIGLE');

        $filename = $this->fileService->computeFileNameForLogoStructure($structure);
        $filename = $this->fileService->computeFileNameForNewLogoStructure($structure);

        $this->assertEquals('SOURCE_CODE-SIGLE.png', $filename);
    }

    public function testCanComputeLogoDirectoryPathForEcoleDoctorale()
    {
        /** @var EcoleDoctorale|\PHPUnit_Framework_MockObject_MockObject $ecoleDoctorale */
        $ecoleDoctorale = $this->createMock(EcoleDoctorale::class);

        $path = $this->fileService->computeDirectoryPathForLogoStructure($ecoleDoctorale);

        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/ED', $path);
    }

    public function testCanComputeLogoDirectoryPathForUniteRecherche()
    {
        /** @var UniteRecherche|\PHPUnit_Framework_MockObject_MockObject $uniteRecherche */
        $uniteRecherche = $this->createMock(UniteRecherche::class);

        $path = $this->fileService->computeDirectoryPathForLogoStructure($uniteRecherche);

        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/UR', $path);
    }

    public function testCanComputeLogoDirectoryPathForEtablissement()
    {
        /** @var Etablissement|\PHPUnit_Framework_MockObject_MockObject $etablissement */
        $etablissement = $this->createMock(Etablissement::class);

        $path = $this->fileService->computeDirectoryPathForLogoStructure($etablissement);

        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/Etab', $path);
    }
//    public function testCanComputeLogoDirectoryPathForEcoleDoctorale()
//    {
//        /** @var EcoleDoctorale|\PHPUnit_Framework_MockObject_MockObject $ecoleDoctorale */
//        $ecoleDoctorale = $this->createMock(EcoleDoctorale::class);
//
//        $path = $this->fileService->computeDirectoryPathForLogoStructure($ecoleDoctorale);
//
//        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/ED', $path);
//    }
//
//    public function testCanComputeLogoDirectoryPathForUniteRecherche()
//    {
//        /** @var UniteRecherche|\PHPUnit_Framework_MockObject_MockObject $uniteRecherche */
//        $uniteRecherche = $this->createMock(UniteRecherche::class);
//
//        $path = $this->fileService->computeDirectoryPathForLogoStructure($uniteRecherche);
//
//        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/UR', $path);
//    }
//
//    public function testCanComputeLogoDirectoryPathForEtablissement()
//    {
//        /** @var Etablissement|\PHPUnit_Framework_MockObject_MockObject $etablissement */
//        $etablissement = $this->createMock(Etablissement::class);
//
//        $path = $this->fileService->computeDirectoryPathForLogoStructure($etablissement);
//
//        $this->assertEquals('/root/directory/path' . '/ressources/Logos' . '/Etab', $path);
//    }

    public function testCanComputeLogoFilepathForEcoleDoctorale()
    {
+35 −27
Changes for module/Fichier/src/Fichier/Service/Fichier/FichierStorageService.php: 35 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -2,9 +2,11 @@

namespace Fichier\Service\Fichier;

use InvalidArgumentException;
use Structure\Entity\Db\EcoleDoctorale;
use Structure\Entity\Db\Etablissement;
use Structure\Entity\Db\Structure;
use Structure\Entity\Db\StructureConcreteInterface;
use Structure\Entity\Db\StructureInterface;
use Structure\Entity\Db\UniteRecherche;
use Fichier\Entity\Db\Fichier;
@@ -171,24 +173,45 @@ class FichierStorageService
    }

    /**
     * Retourne le nom normalisé à donner au fichier physique du logo d'une structure.
     * Retourne le nom normalisé à donner au fichier physique du *nouveau* logo d'une structure.
     *
     * *NB : Cette méthode ne doit pas être utilisée pour obtenir le nom du fichier logo existant d'une structure.
     * Pour cela, il faut utiliser {@see StructureInterface::getCheminLogo()}.*
     *
     * @param StructureInterface $structure Entité Structure concernée
     * @return string
     */
    public function computeFileNameForLogoStructure(StructureInterface $structure): string
    public function computeFileNameForNewLogoStructure(StructureInterface $structure): string
    {
        if ($structure instanceof Etablissement) {
            $name = $structure->getStructure()->getCode() ?: $structure->generateUniqCode();
        } else {
            $name = $structure->getSourceCode() . "-" . $structure->getSigle();
        // Utiliser le 'code' de la structure (plutôt que le 'source_code' et/ou le 'sigle' comme précédemment),
        // qui n'est pas sensé changer, garantit que le calcul du chemin du fichier logo continuera de fonctionner
        // meme si :
        //   - la structure fait l'objet d'une substitution ;
        //   - le sigle de la structure change (ex : 'ED 558 HMPL' => 'ED 558 NH').

        if (!$structure->getCode()) {
            throw new InvalidArgumentException(sprintf(
                "Impossible de calculer le nom du fichier pour le logo de la structure %s car son 'code' est vide",
                $structure->getId()
            ));
        }

        $name = $structure->getCode();

        if ($structure instanceof StructureConcreteInterface) {
            if ($type = $structure->getStructure()->getTypeStructure()) {
                $name = $type->getCode() . '-' . $name;
            }
        }

        $name = str_replace(["'", ':'], '_', $name);
        $name = str_replace(' ', '', $name);

        return $name . ".png";
    }

    /**
     * Retourne le chemin absolu d'une copie ou de l'original (selon le storage) du fichier logo physique d'une structure.
     * Retourne le chemin absolu d'une copie du fichier physique du logo existant d'une structure.
     *
     * @param \Structure\Entity\Db\StructureInterface $structure Entité Structure concernée
     * @return string
@@ -197,7 +220,7 @@ class FichierStorageService
    public function getFileForLogoStructure(StructureInterface $structure): string
    {
        $dirPath = $this->computeDirectoryPathForLogoStructure($structure);
        $fileName = $this->computeFileNameForLogoStructure($structure);
        $fileName = $structure->getCheminLogo();

        $this->storageAdapter->saveToFilesystem($dirPath, $fileName, $tmpFilePath = tempnam(sys_get_temp_dir(), ''));

@@ -205,22 +228,7 @@ class FichierStorageService
    }

    /**
     * Retourne le contenu brut du fichier logo physique d'une structure.
     *
     * @param \Structure\Entity\Db\StructureInterface $structure Entité Structure concernée
     * @return string
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
     */
    public function getFileContentLogoStructure(StructureInterface $structure): string
    {
        $dirPath = $this->computeDirectoryPathForLogoStructure($structure);
        $fileName = $this->computeFileNameForLogoStructure($structure);

        return $this->storageAdapter->getFileContent($dirPath, $fileName);
    }

    /**
     * Enregistre le fichier logo physique d'une structure.
     * Enregistre le fichier physique du nouveau logo d'une structure.
     *
     * @param string $logoFilepath
     * @param \Structure\Entity\Db\StructureInterface $structure
@@ -229,13 +237,13 @@ class FichierStorageService
    public function saveFileForLogoStructure(string $logoFilepath, StructureInterface $structure)
    {
        $logoDir = $this->computeDirectoryPathForLogoStructure($structure);
        $logoFilename = $this->computeFileNameForLogoStructure($structure);
        $logoFilename = $this->computeFileNameForNewLogoStructure($structure);

        $this->storageAdapter->saveFileContent(file_get_contents($logoFilepath), $logoDir, $logoFilename);
    }

    /**
     * Supprime le fichier physique logo d'une structure.
     * Supprime le fichier physique du logo existant d'une structure.
     *
     * @param \Structure\Entity\Db\StructureInterface $structure
     * @throws \Fichier\Service\Storage\Adapter\Exception\StorageAdapterException
@@ -243,7 +251,7 @@ class FichierStorageService
    public function deleteFileForLogoStructure(StructureInterface $structure)
    {
        $dirPath = $this->computeDirectoryPathForLogoStructure($structure);
        $fileName = $this->computeFileNameForLogoStructure($structure);
        $fileName = $structure->getCheminLogo();

        $this->storageAdapter->deleteFile($dirPath, $fileName);
    }
+3 −4
Changes for module/Structure/src/Structure/Entity/Db/EcoleDoctorale.php: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -103,11 +103,10 @@ class EcoleDoctorale
    }

    /**
     * @return string
     * @deprecated mais encore nécessaire à cause de StructureConcreteInterface
     * @see StructureConcreteInterface
     * @return string|null
     */
    public function getCode() {
    public function getCode(): ?string
    {
        return $this->structure->getCode();
    }

+0 −8
Changes for module/Structure/src/Structure/Entity/Db/Etablissement.php: 0 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -108,14 +108,6 @@ class Etablissement
        return $this;
    }

    /**
     * @return string
     */
    public function generateUniqCode()
    {
        return uniqid();
    }

    /**
     * Set sourceCode
     *
+3 −12
Changes for module/Structure/src/Structure/Entity/Db/Structure.php: 3 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -54,10 +54,11 @@ class Structure implements StructureInterface, HistoriqueAwareInterface, SourceA
     * @var string
     */
    protected $sourceCode;

    /**
     * @var string
     * @var string|null
     */
    protected $code;
    protected ?string $code;

    /**
     * @var TypeStructure
@@ -206,16 +207,6 @@ class Structure implements StructureInterface, HistoriqueAwareInterface, SourceA
     */
    public function getCode(): ?string
    {
//        if ($this->code !== null) return $this->code;
//        if ($this->sourceCode !== null) {
//            $code = $this->getSourceCodeStringHelper()->removePrefixFrom($this->sourceCode);
//            return $code;
//        }
        // (!) Les lignes précédentes ont été mises en commentaire car l'entité Structure *possède* un attribut 'code'
        //     donc faire ici autre chose que de retourner l'attribut, c'est du sabotage, voire du terrorisme (spécial dédicace) !!
        //     Preuve que ce n'est pas une bonne idée : impossible de dire dans quels cas c'était nécessaire de faire
        //     ce jonglage avec 'code' et 'sourceCode'.

        return $this->code;
    }

Loading