Skip to content

Commit 3d7ca42

Browse files
committed
refactor release data to DTO for proper type hinting #20
1 parent 5b6f70a commit 3d7ca42

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

app/Jobs/UpdateSite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Models\Site;
88
use App\RemoteSite\Connection;
9+
use App\RemoteSite\Responses\HealthCheck;
910
use App\RemoteSite\Responses\PrepareUpdate;
1011
use Illuminate\Contracts\Queue\ShouldQueue;
1112
use Illuminate\Foundation\Queue\Queueable;

app/TUF/ReleaseData.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\TUF;
4+
5+
use App\DTO\BaseDTO;
6+
7+
class ReleaseData extends BaseDTO
8+
{
9+
public function __construct(
10+
public readonly string $version,
11+
public readonly string $stability
12+
) {
13+
}
14+
}

app/TUF/TufFetcher.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,20 @@ function () {
4141
throw new MetadataException("Empty target custom attribute");
4242
}
4343

44-
return [$target['custom']['version'] => $target['custom']];
44+
$release = ReleaseData::from($target['custom']);
45+
46+
return [$release->version => $release];
4547
});
4648
}
4749
);
4850
}
4951

5052
public function getLatestVersionForBranch(int $branch): string
5153
{
52-
return $this->getReleases()->filter(function ($release) {
53-
return $release["stability"] === "Stable";
54-
})->sort(function ($releaseA, $releaseB) {
55-
return version_compare($releaseA["version"], $releaseB["version"], '<');
54+
return $this->getReleases()->filter(function (ReleaseData $release) {
55+
return $release->stability === "Stable";
56+
})->sort(function (ReleaseData $releaseA, ReleaseData $releaseB) {
57+
return version_compare($releaseA->version, $releaseB->version, '<');
5658
})->pluck('version')->first();
5759
}
5860
}

tests/Unit/TUF/TufFetcherTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Unit\TUF;
44

55
use App\TUF\EloquentModelStorage;
6+
use App\TUF\ReleaseData;
67
use App\TUF\TufFetcher;
78
use Illuminate\Support\Facades\App;
89
use Tests\TestCase;
@@ -24,13 +25,15 @@ public function testGetReleasesConvertsLegitResponse()
2425
"Joomla_5.1.2-Stable-Upgrade_Package.zip" => [
2526
"custom" => [
2627
"description" => "Joomla! 5.1.2 Release",
27-
"version" => "5.1.2"
28+
"version" => "5.1.2",
29+
"stability" => "stable",
2830
]
2931
],
3032
"Joomla_5.2.1-Stable-Upgrade_Package.zip" => [
3133
"custom" => [
3234
"description" => "Joomla! 5.2.1 Release",
33-
"version" => "5.2.1"
35+
"version" => "5.2.1",
36+
"stability" => "stable",
3437
]
3538
]
3639
]));
@@ -39,14 +42,16 @@ public function testGetReleasesConvertsLegitResponse()
3942
$result = $object->getReleases();
4043

4144
$this->assertEquals([
42-
"5.1.2" => [
45+
"5.1.2" => ReleaseData::from([
4346
"description" => "Joomla! 5.1.2 Release",
44-
"version" => "5.1.2"
45-
],
46-
"5.2.1" => [
47+
"version" => "5.1.2",
48+
"stability" => "stable",
49+
]),
50+
"5.2.1" => ReleaseData::from([
4751
"description" => "Joomla! 5.2.1 Release",
48-
"version" => "5.2.1"
49-
],
52+
"version" => "5.2.1",
53+
"stability" => "stable",
54+
]),
5055
], $result->toArray());
5156
}
5257

0 commit comments

Comments
 (0)