Skip to content

Commit

Permalink
Merge pull request #308 from cultuurnet/III-3558-use-datetime-factory
Browse files Browse the repository at this point in the history
III-3558 Use `DateTimeFactory` for consistent return type
  • Loading branch information
LucWollants authored Aug 22, 2024
2 parents 04fb563 + 4877094 commit cfb5de3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 54 deletions.
24 changes: 24 additions & 0 deletions src/DateTimeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\Search;

use DateTimeImmutable;
use DateTimeInterface;
use InvalidArgumentException;

final class DateTimeFactory
{
public static function fromAtom(string $datetime): DateTimeImmutable
{
$format = DateTimeInterface::ATOM;
$object = DateTimeImmutable::createFromFormat($format, $datetime);

if ($object instanceof DateTimeImmutable) {
return $object;
}

throw new InvalidArgumentException($datetime . ' does not appear to be a valid ' . $format . ' datetime string.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument\Properties;

use CultuurNet\UDB3\Search\DateTimeFactory;
use DateTime;
use CultuurNet\UDB3\Search\JsonDocument\JsonTransformer;
use CultuurNet\UDB3\Search\JsonDocument\JsonTransformerLogger;
Expand Down Expand Up @@ -42,7 +43,7 @@ public function transform(array $from, array $draft = []): array
// We could also have a half-open availableRange (without end date), but that would not
// be consistent with existing permanent offers that do have an availableTo set in 2100.
// We also need to set it to 2100-01-01 instead of leaving it open so we can sort on it.
$availableTo = DateTimeImmutable::createFromFormat(DateTime::ATOM, '2100-01-01T00:00:00+00:00');
$availableTo = DateTimeFactory::fromAtom('2100-01-01T00:00:00+00:00');
}

if ($availableFrom > $availableTo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CultuurNet\UDB3\Search\ElasticSearch\JsonDocument\Properties;

use Cake\Chronos\Chronos;
use CultuurNet\UDB3\Search\DateTimeFactory;
use CultuurNet\UDB3\Search\JsonDocument\JsonTransformer;
use CultuurNet\UDB3\Search\JsonDocument\JsonTransformerLogger;
use DateInterval;
Expand Down Expand Up @@ -474,13 +475,13 @@ private function convertSubEventToLocalTimeRanges(array $subEvent, DateTimeZone
// When converting the dates to times it's important we set the right timezone, because sometimes the dates are
// in UTC for example and then the time info is not what we'd expect to be in Belgium.
if (isset($subEvent['startDate'])) {
$startDate = DateTimeImmutable::createFromFormat(DateTime::ATOM, $subEvent['startDate']);
$startDate = DateTimeFactory::fromAtom($subEvent['startDate']);
$startDate = $startDate->setTimezone($timezone);
$startTime = $startDate->format('Hi');
}

if (isset($subEvent['endDate'])) {
$endDate = DateTimeImmutable::createFromFormat(DateTime::ATOM, $subEvent['endDate']);
$endDate = DateTimeFactory::fromAtom($subEvent['endDate']);
$endDate = $endDate->setTimezone($timezone);
$endTime = $endDate->format('Hi');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use CultuurNet\UDB3\Search\Http\ApiRequestInterface;
use CultuurNet\UDB3\Search\Offer\OfferQueryBuilderInterface;
use DateTimeImmutable;
use InvalidArgumentException;

final class AvailabilityOfferRequestParser implements OfferRequestParserInterface
{
public function parse(ApiRequestInterface $request, OfferQueryBuilderInterface $offerQueryBuilder): OfferQueryBuilderInterface
{
$parameterBagReader = $request->getQueryParameterBag();

$default = DateTimeImmutable::createFromFormat('U', (string) $request->getServerParam('REQUEST_TIME', 0))
->format(DATE_ATOM);
$default = DateTimeImmutable::createFromFormat('U', (string) $request->getServerParam('REQUEST_TIME', 0));
if (!$default) {
throw new InvalidArgumentException('Invalid timestamp provided');
}
$default = $default->format(DATE_ATOM);

$availableFrom = $parameterBagReader->getDateTimeFromParameter('availableFrom', $default);
$availableTo = $parameterBagReader->getDateTimeFromParameter('availableTo', $default);
Expand Down
54 changes: 25 additions & 29 deletions tests/ElasticSearch/Offer/ElasticSearchOfferQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CultuurNet\UDB3\Search\Address\PostalCode;
use CultuurNet\UDB3\Search\Country;
use CultuurNet\UDB3\Search\Creator;
use CultuurNet\UDB3\Search\DateTimeFactory;
use CultuurNet\UDB3\Search\ElasticSearch\AbstractElasticSearchQueryBuilderTest;
use CultuurNet\UDB3\Search\ElasticSearch\ElasticSearchDistance;
use CultuurNet\UDB3\Search\ElasticSearch\LuceneQueryString;
Expand Down Expand Up @@ -34,8 +35,6 @@
use CultuurNet\UDB3\Search\SortOrder;
use CultuurNet\UDB3\Search\Start;
use CultuurNet\UDB3\Search\UnsupportedParameterValue;
use DateTime;
use DateTimeImmutable;
use InvalidArgumentException;

final class ElasticSearchOfferQueryBuilderTest extends AbstractElasticSearchQueryBuilderTest
Expand Down Expand Up @@ -437,8 +436,7 @@ public function it_should_build_a_query_with_a_date_range_filter_without_upper_b
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withDateRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
null
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -479,7 +477,7 @@ public function it_should_build_a_query_with_a_date_range_filter_without_lower_b
->withStartAndLimit(new Start(30), new Limit(10))
->withDateRangeFilter(
null,
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -519,8 +517,8 @@ public function it_should_build_a_query_with_a_complete_date_range_filter(): voi
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withDateRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -562,8 +560,8 @@ public function it_should_build_query_with_a_complete_date_range_and_time_range_
->withStartAndLimit(new Start(30), new Limit(10))
->withSubEventFilter(
(new SubEventQueryParameters())
->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00'))
->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00'))
->withLocalTimeFrom(800)
->withLocalTimeTo(1600)
->withStatuses([Status::temporarilyUnavailable(), Status::unavailable()])
Expand Down Expand Up @@ -647,8 +645,8 @@ public function it_should_build_query_with_a_complete_date_range_and_time_range_
->withStartAndLimit(new Start(30), new Limit(10))
->withSubEventFilter(
(new SubEventQueryParameters())
->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00'))
->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00'))
->withLocalTimeFrom(800)
->withLocalTimeTo(1600)
);
Expand Down Expand Up @@ -711,8 +709,8 @@ public function it_should_build_query_with_a_complete_date_range_filter_for_mult
->withStartAndLimit(new Start(30), new Limit(10))
->withSubEventFilter(
(new SubEventQueryParameters())
->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00'))
->withDateFrom(DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'))
->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00'))
->withStatuses([Status::temporarilyUnavailable(), Status::unavailable()])
);

Expand Down Expand Up @@ -1225,8 +1223,7 @@ public function it_should_build_a_query_with_an_available_range_filter_without_u
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
null
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
);

$expectedQueryArray = [
Expand Down Expand Up @@ -1267,7 +1264,7 @@ public function it_should_build_a_query_with_an_available_range_filter_without_l
->withStartAndLimit(new Start(30), new Limit(10))
->withAvailableRangeFilter(
null,
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -1307,8 +1304,8 @@ public function it_should_build_a_query_with_a_complete_available_range_filter()
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -1353,8 +1350,8 @@ public function it_should_throw_an_exception_for_an_invalid_available_range(): v

(new ElasticSearchOfferQueryBuilder())
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00')
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00'),
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00')
);
}

Expand Down Expand Up @@ -2852,8 +2849,7 @@ public function it_should_build_a_query_with_a_created_range_filter_without_uppe
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withCreatedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
null
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
);

$expectedQueryArray = [
Expand Down Expand Up @@ -2894,7 +2890,7 @@ public function it_should_build_a_query_with_a_created_range_filter_without_lowe
->withStartAndLimit(new Start(30), new Limit(10))
->withCreatedRangeFilter(
null,
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -2934,8 +2930,8 @@ public function it_should_build_a_query_with_a_complete_created_range_filter():
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withCreatedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -2976,7 +2972,7 @@ public function it_should_build_a_query_with_a_modified_range_filter_without_upp
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withModifiedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
null
);

Expand Down Expand Up @@ -3018,7 +3014,7 @@ public function it_should_build_a_query_with_a_modified_range_filter_without_low
->withStartAndLimit(new Start(30), new Limit(10))
->withModifiedRangeFilter(
null,
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down Expand Up @@ -3058,8 +3054,8 @@ public function it_should_build_a_query_with_a_complete_modified_range_filter():
$builder = (new ElasticSearchOfferQueryBuilder())
->withStartAndLimit(new Start(30), new Limit(10))
->withModifiedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-25T00:00:00+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+00:00')
DateTimeFactory::fromAtom('2017-04-25T00:00:00+00:00'),
DateTimeFactory::fromAtom('2017-05-01T23:59:59+00:00')
);

$expectedQueryArray = [
Expand Down
35 changes: 17 additions & 18 deletions tests/Http/OfferSearchControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace CultuurNet\UDB3\Search\Http;

use CultuurNet\UDB3\Search\DateTimeFactory;
use InvalidArgumentException;
use CultuurNet\UDB3\Search\Address\PostalCode;
use CultuurNet\UDB3\Search\Country;
Expand Down Expand Up @@ -52,8 +53,6 @@
use CultuurNet\UDB3\Search\Region\RegionId;
use CultuurNet\UDB3\Search\SortOrder;
use CultuurNet\UDB3\Search\Start;
use DateTime;
use DateTimeImmutable;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -249,8 +248,8 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
new WorkflowStatus('DRAFT')
)
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T00:00:00+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-28T15:30:23+01:00')
DateTimeFactory::fromAtom('2017-04-26T00:00:00+01:00'),
DateTimeFactory::fromAtom('2017-04-28T15:30:23+01:00')
)
->withRegionFilter(
$this->regionIndexName,
Expand All @@ -271,18 +270,18 @@ public function it_returns_a_paged_collection_of_search_results_based_on_request
->withUiTPASFilter(true)
->withCreatorFilter(new Creator('Jane Doe'))
->withCreatedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00')
DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00'),
DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00')
)
->withModifiedRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T13:33:37+01:00')
DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00'),
DateTimeFactory::fromAtom('2017-05-01T13:33:37+01:00')
)
->withCalendarTypeFilter(new CalendarType('single'))
->withSubEventFilter(
(new SubEventQueryParameters())
->withDateFrom(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T00:00:00+01:00'))
->withDateTo(DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+01:00'))
->withDateFrom(DateTimeFactory::fromAtom('2017-05-01T00:00:00+01:00'))
->withDateTo(DateTimeFactory::fromAtom('2017-05-01T23:59:59+01:00'))
->withLocalTimeFrom(800)
->withLocalTimeTo(1600)
->withStatuses([Status::unavailable(), Status::temporarilyUnavailable()])
Expand Down Expand Up @@ -486,8 +485,8 @@ public function it_uses_default_parameters_when_default_filters_are_not_disabled
$expectedQueryBuilder = $this->queryBuilder
->withWorkflowStatusFilter(new WorkflowStatus('APPROVED'), new WorkflowStatus('READY_FOR_VALIDATION'))
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T08:34:21+00:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-26T08:34:21+00:00')
DateTimeFactory::fromAtom('2017-04-26T08:34:21+00:00'),
DateTimeFactory::fromAtom('2017-04-26T08:34:21+00:00')
)
->withAddressCountryFilter(new Country('BE'))
->withAudienceTypeFilter(new AudienceType('everyone'))
Expand Down Expand Up @@ -576,12 +575,12 @@ public function it_can_convert_spaces_in_date_parameters_to_plus_signs(): void
$expectedQueryBuilder = $this->queryBuilder
->withStartAndLimit(new Start(30), new Limit(10))
->withAvailableRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T00:00:00+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T23:59:59+01:00')
DateTimeFactory::fromAtom('2017-04-01T00:00:00+01:00'),
DateTimeFactory::fromAtom('2017-04-01T23:59:59+01:00')
)
->withDateRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T00:00:00+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-04-01T23:59:59+01:00')
DateTimeFactory::fromAtom('2017-04-01T00:00:00+01:00'),
DateTimeFactory::fromAtom('2017-04-01T23:59:59+01:00')
);

$expectedResultSet = new PagedResultSet(30, 0, []);
Expand Down Expand Up @@ -1089,8 +1088,8 @@ public function it_uses_a_date_range_filter_only_if_no_status_or_time_from_or_ti

$expectedQueryBuilder = $this->queryBuilder
->withDateRangeFilter(
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T00:00:00+01:00'),
DateTimeImmutable::createFromFormat(DateTime::ATOM, '2017-05-01T23:59:59+01:00')
DateTimeFactory::fromAtom('2017-05-01T00:00:00+01:00'),
DateTimeFactory::fromAtom('2017-05-01T23:59:59+01:00')
);

$expectedResultSet = new PagedResultSet(30, 0, []);
Expand Down
Loading

0 comments on commit cfb5de3

Please sign in to comment.