From 45abd13c95b225c6ef5d64be831df2829601801e Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 3 Dec 2024 15:13:20 +0100 Subject: [PATCH] Improve page fixtures factory date management --- src/Fixture/Factory/PageFixtureFactory.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Fixture/Factory/PageFixtureFactory.php b/src/Fixture/Factory/PageFixtureFactory.php index 416029e..30b56dd 100644 --- a/src/Fixture/Factory/PageFixtureFactory.php +++ b/src/Fixture/Factory/PageFixtureFactory.php @@ -86,15 +86,8 @@ public function create(array $options = []): PageInterface $page->setEnabled($options['enabled']); $page->setCode($options['code']); - $publishAt = $options['publish_at'] ?? null; - if ($publishAt) { - $page->setPublishAt(new DateTime($publishAt)); - } - - $unpublishAt = $options['unpublish_at'] ?? null; - if ($unpublishAt) { - $page->setUnpublishAt(new DateTime($unpublishAt)); - } + $page->setPublishAt($options['publish_at']); + $page->setUnpublishAt($options['unpublish_at']); foreach ($options['channels'] as $channel) { $page->addChannel($channel); @@ -124,6 +117,8 @@ private function createTranslations(PageInterface $page, array $options): void /** * @inheritdoc + * + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function configureOptions(OptionsResolver $resolver): void { @@ -142,9 +137,15 @@ protected function configureOptions(OptionsResolver $resolver): void ->setDefault('publish_at', function (Options $options) use ($publishAt, $hasPublishAt): ?string { return $hasPublishAt ? $publishAt->format('Y-m-d H:i:s') : null; }) + ->setNormalizer('publish_at', function (Options $options, $value): ?DateTime { + return null === $value ? null : new DateTime($value); + }) ->setDefault('unpublish_at', function (Options $options) use ($publishAt): ?string { return $this->faker->boolean(20) ? (clone $publishAt)->modify('+' . $this->faker->numberBetween(1, 20) . ' days')->format('Y-m-d H:i:s') : null; }) + ->setNormalizer('unpublish_at', function (Options $options, $value): ?DateTime { + return null === $value ? null : new DateTime($value); + }) ->setDefault('channels', LazyOption::all($this->channelRepository)) ->setAllowedTypes('channels', 'array') ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))