Updated to provisional hero-common v4.0

This commit is contained in:
Alejandro Celaya 2021-08-04 18:46:19 +02:00
parent 555e6f804c
commit 883ac1007a
13 changed files with 31 additions and 34 deletions

View file

@ -47,7 +47,7 @@
"predis/predis": "^1.1", "predis/predis": "^1.1",
"pugx/shortid-php": "^0.7", "pugx/shortid-php": "^0.7",
"ramsey/uuid": "^3.9", "ramsey/uuid": "^3.9",
"shlinkio/shlink-common": "^3.7", "shlinkio/shlink-common": "dev-main#6e96039 as 4.0",
"shlinkio/shlink-config": "^1.2", "shlinkio/shlink-config": "^1.2",
"shlinkio/shlink-event-dispatcher": "^2.1", "shlinkio/shlink-event-dispatcher": "^2.1",
"shlinkio/shlink-importer": "^2.3.1", "shlinkio/shlink-importer": "^2.3.1",

View file

@ -7,7 +7,6 @@ namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
use Shlinkio\Shlink\CLI\Command\Util\AbstractWithDateRangeCommand; use Shlinkio\Shlink\CLI\Command\Util\AbstractWithDateRangeCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\CLI\Util\ShlinkTable; use Shlinkio\Shlink\CLI\Util\ShlinkTable;
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Model\VisitsParams; use Shlinkio\Shlink\Core\Model\VisitsParams;
@ -21,6 +20,7 @@ use Symfony\Component\Console\Style\SymfonyStyle;
use function Functional\map; use function Functional\map;
use function Functional\select_keys; use function Functional\select_keys;
use function Shlinkio\Shlink\Common\buildDateRange;
use function sprintf; use function sprintf;
class GetVisitsCommand extends AbstractWithDateRangeCommand class GetVisitsCommand extends AbstractWithDateRangeCommand
@ -73,7 +73,7 @@ class GetVisitsCommand extends AbstractWithDateRangeCommand
$paginator = $this->visitsHelper->visitsForShortUrl( $paginator = $this->visitsHelper->visitsForShortUrl(
$identifier, $identifier,
new VisitsParams(new DateRange($startDate, $endDate)), new VisitsParams(buildDateRange($startDate, $endDate)),
); );
$rows = map($paginator->getCurrentPageResults(), function (Visit $visit) { $rows = map($paginator->getCurrentPageResults(), function (Visit $visit) {

View file

@ -45,7 +45,7 @@ class GetVisitsCommandTest extends TestCase
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl( $this->visitsHelper->visitsForShortUrl(
new ShortUrlIdentifier($shortCode), new ShortUrlIdentifier($shortCode),
new VisitsParams(new DateRange(null, null)), new VisitsParams(DateRange::emptyInstance()),
) )
->willReturn(new Paginator(new ArrayAdapter([]))) ->willReturn(new Paginator(new ArrayAdapter([])))
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
@ -61,7 +61,7 @@ class GetVisitsCommandTest extends TestCase
$endDate = '2016-02-01'; $endDate = '2016-02-01';
$this->visitsHelper->visitsForShortUrl( $this->visitsHelper->visitsForShortUrl(
new ShortUrlIdentifier($shortCode), new ShortUrlIdentifier($shortCode),
new VisitsParams(new DateRange(Chronos::parse($startDate), Chronos::parse($endDate))), new VisitsParams(DateRange::withStartAndEndDate(Chronos::parse($startDate), Chronos::parse($endDate))),
) )
->willReturn(new Paginator(new ArrayAdapter([]))) ->willReturn(new Paginator(new ArrayAdapter([])))
->shouldBeCalledOnce(); ->shouldBeCalledOnce();
@ -80,7 +80,7 @@ class GetVisitsCommandTest extends TestCase
$startDate = 'foo'; $startDate = 'foo';
$info = $this->visitsHelper->visitsForShortUrl( $info = $this->visitsHelper->visitsForShortUrl(
new ShortUrlIdentifier($shortCode), new ShortUrlIdentifier($shortCode),
new VisitsParams(new DateRange()), new VisitsParams(DateRange::emptyInstance()),
)->willReturn(new Paginator(new ArrayAdapter([]))); )->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->execute([ $this->commandTester->execute([

View file

@ -16,6 +16,7 @@ use function Functional\reduce_left;
use function is_array; use function is_array;
use function lcfirst; use function lcfirst;
use function print_r; use function print_r;
use function Shlinkio\Shlink\Common\buildDateRange;
use function sprintf; use function sprintf;
use function str_repeat; use function str_repeat;
use function str_replace; use function str_replace;
@ -51,12 +52,7 @@ function parseDateRangeFromQuery(array $query, string $startDateName, string $en
$startDate = parseDateFromQuery($query, $startDateName); $startDate = parseDateFromQuery($query, $startDateName);
$endDate = parseDateFromQuery($query, $endDateName); $endDate = parseDateFromQuery($query, $endDateName);
return match (true) { return buildDateRange($startDate, $endDate);
$startDate === null && $endDate === null => DateRange::emptyInstance(),
$startDate !== null && $endDate !== null => DateRange::withStartAndEndDate($startDate, $endDate),
$startDate !== null => DateRange::withStartDate($startDate),
default => DateRange::withEndDate($endDate),
};
} }
/** /**

View file

@ -8,6 +8,7 @@ use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Exception\ValidationException;
use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter; use Shlinkio\Shlink\Core\Validation\ShortUrlsParamsInputFilter;
use function Shlinkio\Shlink\Common\buildDateRange;
use function Shlinkio\Shlink\Core\parseDateField; use function Shlinkio\Shlink\Core\parseDateField;
final class ShortUrlsParams final class ShortUrlsParams
@ -54,7 +55,7 @@ final class ShortUrlsParams
$this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1); $this->page = (int) ($inputFilter->getValue(ShortUrlsParamsInputFilter::PAGE) ?? 1);
$this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM); $this->searchTerm = $inputFilter->getValue(ShortUrlsParamsInputFilter::SEARCH_TERM);
$this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS); $this->tags = (array) $inputFilter->getValue(ShortUrlsParamsInputFilter::TAGS);
$this->dateRange = new DateRange( $this->dateRange = buildDateRange(
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)), parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::START_DATE)),
parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)), parseDateField($inputFilter->getValue(ShortUrlsParamsInputFilter::END_DATE)),
); );

View file

@ -23,7 +23,7 @@ final class VisitsParams
?int $itemsPerPage = null, ?int $itemsPerPage = null,
private bool $excludeBots = false private bool $excludeBots = false
) { ) {
$this->dateRange = $dateRange ?? new DateRange(); $this->dateRange = $dateRange ?? DateRange::emptyInstance();
$this->page = $this->determinePage($page); $this->page = $this->determinePage($page);
$this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage); $this->itemsPerPage = $this->determineItemsPerPage($itemsPerPage);
} }

View file

@ -105,13 +105,13 @@ class ShortUrlRepository extends EntitySpecificationRepository implements ShortU
$qb->from(ShortUrl::class, 's') $qb->from(ShortUrl::class, 's')
->where('1=1'); ->where('1=1');
if ($dateRange?->getStartDate() !== null) { if ($dateRange?->startDate() !== null) {
$qb->andWhere($qb->expr()->gte('s.dateCreated', ':startDate')); $qb->andWhere($qb->expr()->gte('s.dateCreated', ':startDate'));
$qb->setParameter('startDate', $dateRange->getStartDate(), ChronosDateTimeType::CHRONOS_DATETIME); $qb->setParameter('startDate', $dateRange->startDate(), ChronosDateTimeType::CHRONOS_DATETIME);
} }
if ($dateRange?->getEndDate() !== null) { if ($dateRange?->endDate() !== null) {
$qb->andWhere($qb->expr()->lte('s.dateCreated', ':endDate')); $qb->andWhere($qb->expr()->lte('s.dateCreated', ':endDate'));
$qb->setParameter('endDate', $dateRange->getEndDate(), ChronosDateTimeType::CHRONOS_DATETIME); $qb->setParameter('endDate', $dateRange->endDate(), ChronosDateTimeType::CHRONOS_DATETIME);
} }
// Apply search term to every searchable field if not empty // Apply search term to every searchable field if not empty

View file

@ -187,11 +187,11 @@ class VisitRepository extends EntitySpecificationRepository implements VisitRepo
private function applyDatesInline(QueryBuilder $qb, ?DateRange $dateRange): void private function applyDatesInline(QueryBuilder $qb, ?DateRange $dateRange): void
{ {
if ($dateRange?->getStartDate() !== null) { if ($dateRange?->startDate() !== null) {
$qb->andWhere($qb->expr()->gte('v.date', '\'' . $dateRange->getStartDate()->toDateTimeString() . '\'')); $qb->andWhere($qb->expr()->gte('v.date', '\'' . $dateRange->startDate()->toDateTimeString() . '\''));
} }
if ($dateRange?->getEndDate() !== null) { if ($dateRange?->endDate() !== null) {
$qb->andWhere($qb->expr()->lte('v.date', '\'' . $dateRange->getEndDate()->toDateTimeString() . '\'')); $qb->andWhere($qb->expr()->lte('v.date', '\'' . $dateRange->endDate()->toDateTimeString() . '\''));
} }
} }

View file

@ -20,12 +20,12 @@ class InDateRange extends BaseSpecification
{ {
$criteria = []; $criteria = [];
if ($this->dateRange?->getStartDate() !== null) { if ($this->dateRange?->startDate() !== null) {
$criteria[] = Spec::gte($this->field, $this->dateRange->getStartDate()->toDateTimeString()); $criteria[] = Spec::gte($this->field, $this->dateRange->startDate()->toDateTimeString());
} }
if ($this->dateRange?->getEndDate() !== null) { if ($this->dateRange?->endDate() !== null) {
$criteria[] = Spec::lte($this->field, $this->dateRange->getEndDate()->toDateTimeString()); $criteria[] = Spec::lte($this->field, $this->dateRange->endDate()->toDateTimeString());
} }
return Spec::andX(...$criteria); return Spec::andX(...$criteria);

View file

@ -133,16 +133,16 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
self::assertCount(3, $result); self::assertCount(3, $result);
self::assertSame($bar, $result[0]); self::assertSame($bar, $result[0]);
$result = $this->repo->findList(null, null, null, [], null, new DateRange(null, Chronos::now()->subDays(2))); $result = $this->repo->findList(null, null, null, [], null, DateRange::withEndDate(Chronos::now()->subDays(2)));
self::assertCount(1, $result); self::assertCount(1, $result);
self::assertEquals(1, $this->repo->countList(null, [], new DateRange(null, Chronos::now()->subDays(2)))); self::assertEquals(1, $this->repo->countList(null, [], DateRange::withEndDate(Chronos::now()->subDays(2))));
self::assertSame($foo2, $result[0]); self::assertSame($foo2, $result[0]);
self::assertCount( self::assertCount(
2, 2,
$this->repo->findList(null, null, null, [], null, new DateRange(Chronos::now()->subDays(2))), $this->repo->findList(null, null, null, [], null, DateRange::withStartDate(Chronos::now()->subDays(2))),
); );
self::assertEquals(2, $this->repo->countList(null, [], new DateRange(Chronos::now()->subDays(2)))); self::assertEquals(2, $this->repo->countList(null, [], DateRange::withStartDate(Chronos::now()->subDays(2))));
} }
/** @test */ /** @test */

View file

@ -35,7 +35,7 @@ class VisitsForTagPaginatorAdapterTest extends TestCase
$adapter = $this->createAdapter(null); $adapter = $this->createAdapter(null);
$findVisits = $this->repo->findVisitsByTag( $findVisits = $this->repo->findVisitsByTag(
'foo', 'foo',
new VisitsListFiltering(new DateRange(), false, null, $limit, $offset), new VisitsListFiltering(DateRange::emptyInstance(), false, null, $limit, $offset),
)->willReturn([]); )->willReturn([]);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {

View file

@ -36,7 +36,7 @@ class VisitsPaginatorAdapterTest extends TestCase
$adapter = $this->createAdapter(null); $adapter = $this->createAdapter(null);
$findVisits = $this->repo->findVisitsByShortCode( $findVisits = $this->repo->findVisitsByShortCode(
ShortUrlIdentifier::fromShortCodeAndDomain(''), ShortUrlIdentifier::fromShortCodeAndDomain(''),
new VisitsListFiltering(new DateRange(), false, null, $limit, $offset), new VisitsListFiltering(DateRange::emptyInstance(), false, null, $limit, $offset),
)->willReturn([]); )->willReturn([]);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
@ -54,7 +54,7 @@ class VisitsPaginatorAdapterTest extends TestCase
$adapter = $this->createAdapter($apiKey); $adapter = $this->createAdapter($apiKey);
$countVisits = $this->repo->countVisitsByShortCode( $countVisits = $this->repo->countVisitsByShortCode(
ShortUrlIdentifier::fromShortCodeAndDomain(''), ShortUrlIdentifier::fromShortCodeAndDomain(''),
new VisitsCountFiltering(new DateRange(), false, $apiKey->spec()), new VisitsCountFiltering(DateRange::emptyInstance(), false, $apiKey->spec()),
)->willReturn(3); )->willReturn(3);
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {

View file

@ -53,7 +53,7 @@ class ShortUrlVisitsActionTest extends TestCase
{ {
$shortCode = 'abc123'; $shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(new ShortUrlIdentifier($shortCode), new VisitsParams( $this->visitsHelper->visitsForShortUrl(new ShortUrlIdentifier($shortCode), new VisitsParams(
new DateRange(null, Chronos::parse('2016-01-01 00:00:00')), DateRange::withEndDate(Chronos::parse('2016-01-01 00:00:00')),
3, 3,
10, 10,
), Argument::type(ApiKey::class)) ), Argument::type(ApiKey::class))