Skip to content

Commit

Permalink
Merge pull request #69 from acelaya-forks/feature/support-shlink-4
Browse files Browse the repository at this point in the history
Fix errors when importing from Shlink >=4.0
  • Loading branch information
acelaya authored Mar 28, 2024
2 parents e821b70 + 5915932 commit d0c3607
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
5 changes: 3 additions & 2 deletions src/Sources/Shlink/ShlinkImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Sources/Shlink/ShlinkMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
Expand Down
15 changes: 9 additions & 6 deletions test/Sources/Shlink/ShlinkImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit d0c3607

Please sign in to comment.