From 5915932c8a3f41cadad87a8bd494705b746af309 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 28 Mar 2024 13:42:18 +0100 Subject: [PATCH] Fix errors when importing from Shlink >=4.0 --- CHANGELOG.md | 17 +++++++++++++++++ src/Sources/Shlink/ShlinkImporter.php | 5 +++-- src/Sources/Shlink/ShlinkMapper.php | 2 +- test/Sources/Shlink/ShlinkImporterTest.php | 15 +++++++++------ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 467bd62..8202bae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## [5.3.1] - 2024-03-28 +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* Fix errors when importing short URLs using Shlink strategy from a Shlink 4 instance + + ## [5.3.0] - 2024-03-03 ### Added * *Nothing* diff --git a/src/Sources/Shlink/ShlinkImporter.php b/src/Sources/Shlink/ShlinkImporter.php index 4e9b429..11c4f81 100644 --- a/src/Sources/Shlink/ShlinkImporter.php +++ b/src/Sources/Shlink/ShlinkImporter.php @@ -106,7 +106,7 @@ private function mapUrls(array $urls, ShlinkParams $params): array // reverse them. // In order to do that, we calculate the amount of pages we will get, and start from last to first. // Then, each page's result set gets reversed individually. - $visitsCount = $url['visitsCount']; + $visitsCount = $url['visitsCount'] ?? $url['visitsSummary']['total']; $expectedPages = (int) ceil($visitsCount / self::VISITS_PER_PAGE); return $this->mapper->mapShortUrl( @@ -176,8 +176,9 @@ private function getOrphanVisitsCount(ShlinkParams $params): int $url, ['X-Api-Key' => $params->apiKey, 'Accept' => 'application/json'], ); + $visits = $parsedBody['visits'] ?? []; - return (int) ($parsedBody['visits']['orphanVisitsCount'] ?? 0); + return (int) ($visits['orphanVisitsCount'] ?? $visits['orphanVisits']['total'] ?? 0); } private function loadOrphanVisits(ShlinkParams $params, int $page): iterable diff --git a/src/Sources/Shlink/ShlinkMapper.php b/src/Sources/Shlink/ShlinkMapper.php index 50d7829..a8def7a 100644 --- a/src/Sources/Shlink/ShlinkMapper.php +++ b/src/Sources/Shlink/ShlinkMapper.php @@ -32,7 +32,7 @@ public function mapShortUrl(array $url, iterable $visits, DateTimeInterface $fal $url['shortCode'], $url['title'] ?? null, $visits, - $url['visitsCount'], + $url['visitsCount'] ?? $url['visitsSummary']['total'], $meta, ); } diff --git a/test/Sources/Shlink/ShlinkImporterTest.php b/test/Sources/Shlink/ShlinkImporterTest.php index 87e0422..43e538b 100644 --- a/test/Sources/Shlink/ShlinkImporterTest.php +++ b/test/Sources/Shlink/ShlinkImporterTest.php @@ -8,6 +8,7 @@ use PHPUnit\Framework\Assert; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use PHPUnit\Framework\Attributes\TestWith; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -58,7 +59,9 @@ public function expectedAmountOfCallsIsPerformedBasedOnPaginationResults( 'shortUrl' => 'https://acel.me/rY9zd', 'longUrl' => 'https://www.alejandrocelaya.com/foo', 'dateCreated' => '2016-05-02T17:49:53+02:00', - 'visitsCount' => 48, + 'visitsSummary' => [ + 'total' => 48, + ], 'tags' => ['bar', 'foo', 'website'], 'meta' => [ 'validUntil' => '2020-05-02T17:49:53+02:00', @@ -216,7 +219,9 @@ function (string $url) use (&$urlsCallNum, $shortUrl): array { } #[Test] - public function orphanVisitsAreImportedWhenRequested(): void + #[TestWith([['orphanVisitsCount' => 800]])] + #[TestWith([['orphanVisits' => ['total' => 800]]])] + public function orphanVisitsAreImportedWhenRequested(array $visitsOverview): void { $visit1 = [ 'referer' => 'visit1', @@ -244,11 +249,9 @@ public function orphanVisitsAreImportedWhenRequested(): void ]; $this->apiConsumer->expects($this->exactly(4))->method('callApi')->willReturnCallback( - function (string $url) use ($visit1, $visit2) { + function (string $url) use ($visit1, $visit2, $visitsOverview) { if (! str_contains($url, 'orphan')) { - return [ - 'visits' => ['orphanVisitsCount' => 800], - ]; + return ['visits' => $visitsOverview]; } return [