Skip to content

Commit

Permalink
refactor release data to DTO for proper type hinting #20
Browse files Browse the repository at this point in the history
  • Loading branch information
SniperSister committed Jan 18, 2025
1 parent 5b6f70a commit 3d7ca42
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/Jobs/UpdateSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Models\Site;
use App\RemoteSite\Connection;
use App\RemoteSite\Responses\HealthCheck;
use App\RemoteSite\Responses\PrepareUpdate;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
Expand Down
14 changes: 14 additions & 0 deletions app/TUF/ReleaseData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\TUF;

use App\DTO\BaseDTO;

class ReleaseData extends BaseDTO
{
public function __construct(
public readonly string $version,
public readonly string $stability
) {
}
}
12 changes: 7 additions & 5 deletions app/TUF/TufFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ function () {
throw new MetadataException("Empty target custom attribute");
}

return [$target['custom']['version'] => $target['custom']];
$release = ReleaseData::from($target['custom']);

return [$release->version => $release];
});
}
);
}

public function getLatestVersionForBranch(int $branch): string
{
return $this->getReleases()->filter(function ($release) {
return $release["stability"] === "Stable";
})->sort(function ($releaseA, $releaseB) {
return version_compare($releaseA["version"], $releaseB["version"], '<');
return $this->getReleases()->filter(function (ReleaseData $release) {

Check failure on line 54 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\TUF\TufFetcher::getLatestVersionForBranch() should return string but returns mixed.

Check failure on line 54 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),mixed>::filter() expects (callable(mixed, int|string): bool)|null, Closure(App\TUF\ReleaseData): bool given.
return $release->stability === "Stable";
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB) {

Check failure on line 56 in app/TUF/TufFetcher.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),mixed>::sort() expects (callable(mixed, mixed): int)|int|null, Closure(App\TUF\ReleaseData, App\TUF\ReleaseData): bool given.
return version_compare($releaseA->version, $releaseB->version, '<');
})->pluck('version')->first();
}
}
21 changes: 13 additions & 8 deletions tests/Unit/TUF/TufFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Unit\TUF;

use App\TUF\EloquentModelStorage;
use App\TUF\ReleaseData;
use App\TUF\TufFetcher;
use Illuminate\Support\Facades\App;
use Tests\TestCase;
Expand All @@ -24,13 +25,15 @@ public function testGetReleasesConvertsLegitResponse()
"Joomla_5.1.2-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.1.2 Release",
"version" => "5.1.2"
"version" => "5.1.2",
"stability" => "stable",
]
],
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
"custom" => [
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1"
"version" => "5.2.1",
"stability" => "stable",
]
]
]));
Expand All @@ -39,14 +42,16 @@ public function testGetReleasesConvertsLegitResponse()
$result = $object->getReleases();

$this->assertEquals([
"5.1.2" => [
"5.1.2" => ReleaseData::from([
"description" => "Joomla! 5.1.2 Release",
"version" => "5.1.2"
],
"5.2.1" => [
"version" => "5.1.2",
"stability" => "stable",
]),
"5.2.1" => ReleaseData::from([
"description" => "Joomla! 5.2.1 Release",
"version" => "5.2.1"
],
"version" => "5.2.1",
"stability" => "stable",
]),
], $result->toArray());
}

Expand Down

0 comments on commit 3d7ca42

Please sign in to comment.