Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Application/Import/CMCSVMouvementsImportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementCollection;
use App\Domain\Mouvement\MouvementId;
use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;

/**
* Implémente un handler CSV d'import de mouvements de la banque Crédit Mutuel.
Expand Down Expand Up @@ -71,8 +73,12 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection
// Si on a dépassé la ligne d'en-têtes
if ($currentLine > $headersLine) {
// Si la date est valide et sans month shifting
$date = \DateTimeImmutable::createFromFormat('d/m/Y', (string) $cols[0]);
$isValidDate = $date instanceof \DateTimeImmutable && false === $date->getLastErrors();
try {
Date::créer('d/m/Y H:i:s', "{$cols[0]} 00:00:00");
$isValidDate = true;
} catch (DateInvalideException) {
$isValidDate = false;
}

// Alors la ligne en cours est un mouvement
if ($isValidDate) {
Expand All @@ -89,9 +95,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection

foreach ($rows as $row) {
// Date
$date = \DateTimeImmutable::createFromFormat('d/m/Y H:i:s', "{$row['date_operation']} 00:00:00");
if (!$date instanceof \DateTimeImmutable) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date_operation']));
try {
$date = Date::créer('d/m/Y H:i:s', "{$row['date_operation']} 00:00:00");
} catch (DateInvalideException $e) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date_operation']), previous: $e);
}

// Montant
Expand Down
12 changes: 5 additions & 7 deletions src/Application/Import/CMExcelMouvementsImportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementCollection;
use App\Domain\Mouvement\MouvementId;
use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;
use Shuchkin\SimpleXLSX;

/**
Expand Down Expand Up @@ -103,14 +105,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection
$montant = sprintf('%0.2f', $montant);
$montant = new Montant((float) $montant);

// Date : une cellule sans format de date donne un nombre, une cellule vide une chaîne vide, une cellule absente null
if (!is_string($date) || '' === $date) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide en ligne [%d] de la feuille [%s] : [%s].', $rowNumber, $sheetName, $date));
}

// Date : une cellule de date donne une chaîne Y-m-d H:i:s, une cellule sans format de date un nombre, une cellule vide une chaîne vide, une cellule absente null
try {
$dateMouvement = new \DateTimeImmutable($date);
} catch (\DateMalformedStringException $e) {
$dateMouvement = Date::créer('Y-m-d H:i:s', (string) $date);
} catch (DateInvalideException $e) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide en ligne [%d] de la feuille [%s] : [%s].', $rowNumber, $sheetName, $date), previous: $e);
}

Expand Down
17 changes: 12 additions & 5 deletions src/Application/Import/CaisseEpargneCSVMouvementsImportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementCollection;
use App\Domain\Mouvement\MouvementId;
use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;

/**
* Implémente un handler CSV d'import de mouvements de la banque Caisse d'Épargne.
Expand Down Expand Up @@ -72,8 +74,12 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection
// Si on a dépassé la ligne d'en-têtes
if ($currentLine > $headersLine) {
// Si la date est valide et sans month shifting
$date = \DateTimeImmutable::createFromFormat('d/m/y', (string) $cols[0]);
$isValidDate = $date instanceof \DateTimeImmutable && false === $date->getLastErrors();
try {
Date::créer('d/m/y H:i:s', "{$cols[0]} 00:00:00");
$isValidDate = true;
} catch (DateInvalideException) {
$isValidDate = false;
}

// Alors la ligne en cours est un mouvement
if ($isValidDate) {
Expand All @@ -90,9 +96,10 @@ protected function lireMouvements(\SplFileObject $file): MouvementCollection

foreach ($rows as $row) {
// Date
$date = \DateTimeImmutable::createFromFormat('d/m/y H:i:s', "{$row['date']} 00:00:00");
if (!$date instanceof \DateTimeImmutable) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date']));
try {
$date = Date::créer('d/m/y H:i:s', "{$row['date']} 00:00:00");
} catch (DateInvalideException $e) {
throw new FichierImportInvalideException(sprintf('Date du mouvement invalide : [%s].', $row['date']), previous: $e);
}

// Montant
Expand Down
32 changes: 32 additions & 0 deletions src/Domain/Temps/Date.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Domain\Temps;

final class Date extends \DateTimeImmutable
{
/**
* Une date se crée par créer() : le constructeur hérité ferait du month shifting.
*/
private function __construct()
{
}

/**
* Crée une date stricte, sans month shifting : createFromFormat décale les dates impossibles (31-02 devient 03-03)
* et lit 26 comme l'an 26, la date n'est donc valide que si, relue au même format, elle redonne la chaîne reçue.
*
* @throws DateInvalideException
*/
public static function créer(string $format, string $date): self
{
$dateTime = self::createFromFormat($format, $date);

if (!$dateTime instanceof self || $dateTime->format($format) !== $date) {
throw new DateInvalideException(sprintf('Date [%s] invalide au format [%s].', $date, $format));
}

return $dateTime;
}
}
9 changes: 9 additions & 0 deletions src/Domain/Temps/DateInvalideException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace App\Domain\Temps;

final class DateInvalideException extends \InvalidArgumentException
{
}
11 changes: 6 additions & 5 deletions src/Infrastructure/Controller/MouvementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementId;
use App\Domain\Mouvement\MouvementRepositoryInterface;
use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -58,11 +60,10 @@ public function __invoke(Request $request): RedirectResponse
// Date
if (isset($mouvementArray['date'])) {
$dateString = $mouvementArray['date'];
$date = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateString 00:00:00");

// createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 26 comme l'an 26 : la date relue doit être celle saisie
if (!$date instanceof \DateTimeImmutable || $date->format('d-m-Y') !== $dateString) {
throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString));
try {
$date = Date::créer('d-m-Y H:i:s', "$dateString 00:00:00");
} catch (DateInvalideException $e) {
throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString), $e);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/Infrastructure/Controller/MouvementsImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementCollection;
use App\Domain\Mouvement\MouvementRepositoryInterface;
use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;
use App\Infrastructure\Configuration\ConfigurationLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -190,10 +192,10 @@ public function import(Request $request): Response
// Modification éventuelle de la date
if (isset($mouvementsData[$id]['date'])) {
$dateString = $mouvementsData[$id]['date'];
$date = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateString 00:00:00");
// createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 26 comme l'an 26 : la date relue doit être celle saisie
if (!$date instanceof \DateTimeImmutable || $date->format('d-m-Y') !== $dateString) {
throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString));
try {
$date = Date::créer('d-m-Y H:i:s', "$dateString 00:00:00");
} catch (DateInvalideException $e) {
throw new BadRequestHttpException(sprintf('Date du mouvement invalide : [%s].', $dateString), $e);
}
$mouvement->date = $date;
}
Expand Down
11 changes: 3 additions & 8 deletions src/Infrastructure/Denormalizer/CompteDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@
use App\Domain\Compte\CompteId;
use App\Domain\Compte\Plafond;
use App\Domain\Compte\Solde;
use App\Domain\Temps\Date;

final readonly class CompteDenormalizer implements Denormalizer
{
public function denormalize(array $data): Compte
{
$dateOuverture = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date_ouverture']} 00:00:00");
if (!$dateOuverture instanceof \DateTimeImmutable) {
throw new \UnexpectedValueException(sprintf("Date d'ouverture du compte invalide : [%s].", $data['date_ouverture']));
}
$dateOuverture = Date::créer('Y-m-d H:i:s', "{$data['date_ouverture']} 00:00:00");

$dateFermeture = null;
if (is_string($data['date_fermeture'])) {
$dateFermeture = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date_fermeture']} 00:00:00");
if (!$dateFermeture instanceof \DateTimeImmutable) {
throw new \UnexpectedValueException(sprintf('Date de fermeture du compte invalide : [%s].', $data['date_fermeture']));
}
$dateFermeture = Date::créer('Y-m-d H:i:s', "{$data['date_fermeture']} 00:00:00");
}

return new Compte(
Expand Down
6 changes: 2 additions & 4 deletions src/Infrastructure/Denormalizer/MouvementDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Domain\Mouvement\Montant;
use App\Domain\Mouvement\Mouvement;
use App\Domain\Mouvement\MouvementId;
use App\Domain\Temps\Date;

final readonly class MouvementDenormalizer implements Denormalizer
{
Expand All @@ -18,10 +19,7 @@ public function __construct(

public function denormalize(array $data): Mouvement
{
$date = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', "{$data['date']} 00:00:00");
if (!$date instanceof \DateTimeImmutable) {
throw new \UnexpectedValueException(sprintf('Date du mouvement invalide : [%s].', $data['date']));
}
$date = Date::créer('Y-m-d H:i:s', "{$data['date']} 00:00:00");

return new Mouvement(
new MouvementId((string) $data['id']),
Expand Down
34 changes: 12 additions & 22 deletions src/Infrastructure/ValueResolver/PeriodeValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Infrastructure\ValueResolver;

use App\Domain\Temps\Date;
use App\Domain\Temps\DateInvalideException;
use App\Domain\Temps\Depuis;
use App\Domain\Temps\Periode;
use Symfony\Component\HttpFoundation\InputBag;
Expand All @@ -27,15 +29,14 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable
$dateStartString = $dateFilter->getString('start');
$dateEndString = $dateFilter->getString('end');

$dateStart = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateStartString 00:00:00");
$dateEnd = \DateTimeImmutable::createFromFormat('d-m-Y H:i:s', "$dateEndString 23:59:59");

if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) {
throw new BadRequestHttpException('La période de dates est invalide.');
try {
$dateStart = Date::créer('d-m-Y H:i:s', "$dateStartString 00:00:00");
$dateEnd = Date::créer('d-m-Y H:i:s', "$dateEndString 23:59:59");
} catch (DateInvalideException $e) {
throw new BadRequestHttpException('La période de dates est invalide.', $e);
}

// createFromFormat décale les dates impossibles (31-02 devient 03-03) et lit 25 comme l'an 25 : la date relue doit être celle reçue
if ($dateStart->format('d-m-Y') !== $dateStartString || $dateEnd->format('d-m-Y') !== $dateEndString) {
if ($dateStart > $dateEnd) {
throw new BadRequestHttpException('La période de dates est invalide.');
}

Expand Down Expand Up @@ -70,15 +71,8 @@ public function depuisUnAn(): Periode
$year = (int) $year;
$lastDayOfMonth = (int) $lastDayOfMonth;

$dateStart = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-1 00:00:00");
if ($dateStart instanceof \DateTimeImmutable) {
$dateStart = $dateStart->modify('-1 year')->setTime(0, 0); // Depuis un an
}
$dateEnd = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59");

if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) {
throw new BadRequestHttpException('La période de dates est invalide.');
}
$dateStart = Date::créer('Y-n-j H:i:s', "$year-$month-1 00:00:00")->modify('-1 year')->setTime(0, 0); // Depuis un an
$dateEnd = Date::créer('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59");

return new Periode($dateStart, $dateEnd);
}
Expand All @@ -94,12 +88,8 @@ public function depuisUnMois(): Periode
$year = (int) $year;
$lastDayOfMonth = (int) $lastDayOfMonth;

$dateStart = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-1 00:00:00");
$dateEnd = \DateTimeImmutable::createFromFormat('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59");

if (!($dateStart instanceof \DateTimeImmutable) || !($dateEnd instanceof \DateTimeImmutable) || $dateStart > $dateEnd) {
throw new BadRequestHttpException('La période de dates est invalide.');
}
$dateStart = Date::créer('Y-n-j H:i:s', "$year-$month-1 00:00:00");
$dateEnd = Date::créer('Y-n-j H:i:s', "$year-$month-$lastDayOfMonth 23:59:59");

return new Periode($dateStart, $dateEnd);
}
Expand Down