Skip to content

Commit

Permalink
Merge pull request #3054 from MTES-MCT/feature/2947-new-structure-des…
Browse files Browse the repository at this point in the history
…ordre

[BO - Oilhi] Gestion de la nouvelle structure des désordres
  • Loading branch information
emilschn authored Sep 23, 2024
2 parents f4b94ea + cc5f609 commit 786bfc4
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 86 deletions.
104 changes: 25 additions & 79 deletions src/Factory/Interconnection/Oilhi/DossierMessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
namespace App\Factory\Interconnection\Oilhi;

use App\Entity\Affectation;
use App\Entity\Critere;
use App\Entity\Criticite;
use App\Entity\DesordreCategorie;
use App\Entity\DesordreCritere;
use App\Entity\DesordrePrecision;
use App\Entity\Enum\OccupantLink;
use App\Entity\Enum\Qualification;
use App\Entity\Intervention;
use App\Entity\Signalement;
use App\Entity\Situation;
use App\Factory\Interconnection\DossierMessageFactoryInterface;
use App\Messenger\Message\Oilhi\DossierMessage;
use App\Service\HtmlCleaner;
use App\Service\Oilhi\Model\Desordre;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

Expand All @@ -24,9 +20,9 @@ class DossierMessageFactory implements DossierMessageFactoryInterface
public const FORMAT_DATE = 'Y-m-d';

public function __construct(
private UrlGeneratorInterface $urlGenerator,
private readonly UrlGeneratorInterface $urlGenerator,
#[Autowire(env: 'FEATURE_OILHI_ENABLE')]
private bool $featureEnable,
private readonly bool $featureEnable,
) {
}

Expand All @@ -45,7 +41,6 @@ public function createInstance(Affectation $affectation): DossierMessage
$signalement = $affectation->getSignalement();
$partner = $affectation->getPartner();
$interventionData = $this->buildInterventionData($signalement);
$desordresData = $this->buildDesordresData($signalement);
$typeDeclarant = $this->getTypeDeclarant($signalement);

return (new DossierMessage())
Expand Down Expand Up @@ -89,9 +84,7 @@ public function createInstance(Affectation $affectation): DossierMessage
->setRapportVisite($interventionData['rapport_visite'] ?? null)
->setDateVisite($interventionData['date_visite'] ?? null)
->setOperateurVisite($interventionData['operateur_visite'] ?? null)
->setDesordresCategorie($desordresData['categories'] ?? null)
->setDesordresCritere($desordresData['criteres'] ?? null)
->setDesordresPrecision($desordresData['precisions'] ?? null);
->setDesordres($this->buildDesordres($signalement));
}

/**
Expand All @@ -116,85 +109,38 @@ private function buildInterventionData(Signalement $signalement): ?array
}

/**
* @return array Un tableau associatif contenant les libéllés des désordres pour ancien et nouveau formulaire
* - 'categories': Les catégories de désordres/les situations.
* - 'criteres': Les critères de désordres.
* - 'precisions': Les précisions sur les désordres/les criticités.
* @return array|Desordre[]
*/
private function buildDesordresData(Signalement $signalement): array
private function buildDesordres(Signalement $signalement): array
{
/** @var Desordre[] $desordres */
$desordres = [];
if ($signalement->getCreatedFrom()) {
if (!$signalement->getDesordreCategories()->isEmpty()) {
$desordres['categories'] = implode(
',',
$signalement
->getDesordreCategories()
->map(function (DesordreCategorie $desordreCategorie) {
return $desordreCategorie->getLabel();
})->toArray()
foreach ($signalement->getDesordrePrecisions() as $desordrePrecision) {
$desordre = new Desordre(
desordre: $desordrePrecision->getDesordreCritere()->getDesordreCategorie()->getLabel(),
equipement: $desordrePrecision->getDesordreCritere()->getLabelCritere(),
risque: HtmlCleaner::clean($desordrePrecision->getLabel()),
isDanger: $desordrePrecision->getIsDanger(),
isSurrocupation: $desordrePrecision->getIsSuroccupation(),
isInsalubrite: $desordrePrecision->getIsInsalubrite()
);
}

if (!$signalement->getDesordreCriteres()->isEmpty()) {
$desordres['criteres'] = implode(
',',
$signalement
->getDesordreCriteres()
->map(function (DesordreCritere $desordreCritere) {
return $desordreCritere->getLabelCritere();
})->toArray()
);
}

if (!$signalement->getDesordrePrecisions()->isEmpty()) {
$precisionsList = $signalement
->getDesordrePrecisions()
->filter(function (DesordrePrecision $desordrePrecision) {
return ' - ' !== $desordrePrecision->getLabel();
})
->map(function (DesordrePrecision $desordrePrecision) {
if (!empty($desordrePrecision->getLabel())) {
return HtmlCleaner::clean($desordrePrecision->getLabel());
}
})->toArray();
$desordres['precisions'] = implode(',', array_filter($precisionsList));
$desordres[] = $desordre;
}

return $desordres;
}

if (!$signalement->getSituations()->isEmpty()) {
$desordres['categories'] = implode(
',',
$signalement
->getSituations()
->map(function (Situation $situation) {
return $situation->getLabel();
})->toArray()
);
}

if (!$signalement->getCriteres()->isEmpty()) {
$desordres['criteres'] = implode(
',',
$signalement
->getCriteres()
->map(function (Critere $critere) {
return $critere->getLabel();
})->toArray()
);
}

if (!$signalement->getCriticites()->isEmpty()) {
$desordres['precisions'] = implode(
',',
$signalement
->getCriticites()
->map(function (Criticite $criticite) {
return $criticite->getLabel();
})->toArray()
$criticites = $signalement->getCriticites();
/** @var Criticite $criticite */
foreach ($criticites as $criticite) {
$desordre = new Desordre(
desordre: $criticite->getCritere()->getSituation()->getLabel(),
equipement: $criticite->getCritere()->getLabel(),
risque: $criticite->getLabel(),
isDanger: $criticite->getIsDanger(),
);
$desordres[] = $desordre;
}

return $desordres;
Expand Down
13 changes: 13 additions & 0 deletions src/Messenger/Message/Oilhi/DossierMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final class DossierMessage implements DossierMessageInterface
private ?string $rapportVisite = null;
private ?string $dateVisite = null;
private ?string $operateurVisite = null;
private array $desordres = [];

public function __construct()
{
Expand Down Expand Up @@ -375,4 +376,16 @@ public function setOperateurVisite(?string $operateurVisite): self

return $this;
}

public function getDesordres(): array
{
return $this->desordres;
}

public function setDesordres(array $desordres): self
{
$this->desordres = $desordres;

return $this;
}
}
46 changes: 46 additions & 0 deletions src/Service/Oilhi/Model/Desordre.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Service\Oilhi\Model;

readonly class Desordre
{
public function __construct(
private ?string $desordre = null,
private ?string $equipement = null,
private ?string $risque = null,
private ?bool $isDanger = null,
private ?bool $isSurrocupation = null,
private ?bool $isInsalubrite = null,
) {
}

public function getDesordre(): ?string
{
return $this->desordre;
}

public function getEquipement(): ?string
{
return $this->equipement;
}

public function getRisque(): ?string
{
return $this->risque;
}

public function getIsDanger(): ?bool
{
return $this->isDanger;
}

public function getIsSurrocupation(): ?bool
{
return $this->isSurrocupation;
}

public function getIsInsalubrite(): ?bool
{
return $this->isInsalubrite;
}
}
54 changes: 51 additions & 3 deletions tests/Functional/Factory/Oilhi/DossierMessageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,40 @@
use App\Entity\Partner;
use App\Entity\Signalement;
use App\Factory\Interconnection\Oilhi\DossierMessageFactory;
use App\Messenger\Message\Oilhi\DossierMessage;
use App\Repository\PartnerRepository;
use App\Repository\SignalementRepository;
use App\Service\Oilhi\HookZapierService;
use App\Tests\FixturesHelper;
use Doctrine\ORM\EntityManagerInterface;
use Faker\Factory;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class DossierMessageFactoryTest extends KernelTestCase
{
use FixturesHelper;

private EntityManagerInterface $entityManager;
private LoggerInterface $logger;
private SerializerInterface $serializer;

private const PATTERN_EXPECTED_DATE_FORMAT = '/^\d{4}-\d{2}-\d{2}$/';

protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get('doctrine')->getManager();
$this->logger = self::getContainer()->get('logger');
$this->serializer = self::getContainer()->get('serializer');
}

/** @dataProvider provideReference */
Expand Down Expand Up @@ -66,9 +81,6 @@ public function testDossierMessageFullyCreated(string $reference): void
$this->assertNotEmpty($dossierMessage->getCourrielDeclarant());

$this->assertEquals('⌛️ Procédure en cours', $dossierMessage->getStatut());
$this->assertNotEmpty($dossierMessage->getDesordresCategorie());
$this->assertNotEmpty($dossierMessage->getDesordresCritere());
$this->assertNotEmpty($dossierMessage->getDesordresPrecision());

$this->assertMatchesRegularExpression(
self::PATTERN_EXPECTED_DATE_FORMAT,
Expand All @@ -84,6 +96,42 @@ public function testDossierMessageFullyCreated(string $reference): void
$this->assertCount(2, explode(',', $dossierMessage->getCourrielContributeurs()));
}

/**
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ClientExceptionInterface
*/
public function testPushDossierWithException()
{
$faker = Factory::create();

// Throw an exception when the HTTP client is used
$mockHttpClient = $this->createMock(HttpClientInterface::class);
$mockHttpClient->method('request')
->willThrowException(new TransportException('HTTP request failed'));

$hookZapierService = new HookZapierService(
$mockHttpClient,
$this->logger,
$this->serializer,
'ZAPIER_OILHI_TOKEN',
'USER_ID',
'ZAP_ID',
);

$dossierMessage = (new DossierMessage())
->setPartnerId(1)
->setSignalementId(1)
->setSignalementUrl($faker->url());

$response = $hookZapierService->pushDossier($dossierMessage);

$responseData = json_decode($response->getContent(), true);
$this->assertArrayHasKey('message', $responseData);
$this->assertEquals('HTTP request failed', $responseData['message']);
}

public function provideReference(): \Generator
{
yield 'Dossier avec l\'ancien formulaire 2024-01' => ['2024-01'];
Expand Down
19 changes: 17 additions & 2 deletions tests/Unit/Service/Oilhi/HookZapierServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

use App\Messenger\Message\Oilhi\DossierMessage;
use App\Service\Oilhi\HookZapierService;
use App\Service\Oilhi\Model\Desordre;
use Faker\Factory;
use Monolog\Test\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

class HookZapierServiceTest extends TestCase
{
Expand All @@ -21,6 +24,9 @@ protected function setUp(): void
$this->logger = $this->createMock(LoggerInterface::class);
}

/**
* @throws TransportExceptionInterface
*/
public function testPushDossierWithSuccess()
{
$faker = Factory::create();
Expand All @@ -32,12 +38,12 @@ public function testPushDossierWithSuccess()
];
$mockResponse = new MockResponse($response);

$normalizer = new ObjectNormalizer();
$serializer = new Serializer([new ObjectNormalizer()]);
$mockHttpClient = new MockHttpClient($mockResponse);
$hookZapierService = new HookZapierService(
$mockHttpClient,
$this->logger,
$normalizer,
$serializer,
'ZAPIER_OILHI_TOKEN',
'USER_ID',
'ZAP_ID',
Expand All @@ -46,6 +52,15 @@ public function testPushDossierWithSuccess()
$dossierMessage = (new DossierMessage())
->setPartnerId(1)
->setSignalementId(1)
->setDesordres([
new Desordre(
'categorie',
'equipement',
'risque',
true,
false,
true)]
)
->setSignalementUrl($faker->url());

$response = $hookZapierService->pushDossier($dossierMessage);
Expand Down
Loading

0 comments on commit 786bfc4

Please sign in to comment.