Skip to content

Commit a356131

Browse files
authored
Chore: Rename downloads class (#68)
1 parent 4a3a53c commit a356131

12 files changed

+88
-66
lines changed

src/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ protected function build(ContainerBuilder $container): void
7070

7171
private function getAppKey(): string
7272
{
73-
return md5(file_get_contents(__FILE__) . '3');
73+
return md5(file_get_contents(__FILE__) . '4');
7474
}
7575
}

src/Command/DownloadCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Command;
44

5-
use App\DTO\DownloadDescription;
5+
use App\DTO\GameInstaller;
66
use App\DTO\GameExtra;
77
use App\Enum\Language;
88
use App\Enum\NamingConvention;
@@ -181,10 +181,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
181181
$download,
182182
$io,
183183
) {
184-
assert($download instanceof DownloadDescription || $download instanceof GameExtra);
184+
assert($download instanceof GameInstaller || $download instanceof GameExtra);
185185

186186
$downloadTag = new LatelyBoundStringValue(function () use ($download, $game) {
187-
if ($download instanceof DownloadDescription) {
187+
if ($download instanceof GameInstaller) {
188188
return "[{$game->title}] {$download->name} ({$download->platform}, {$download->language})";
189189
} else if ($download instanceof GameExtra) {
190190
return "[{$game->title}] {$download->name} (extra)";

src/DTO/DownloadDescription.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
<?php
22

3-
namespace App\DTO;
3+
use App\DTO\GameInstaller;
44

5-
final class DownloadDescription
6-
{
7-
public function __construct(
8-
public readonly string $language,
9-
public readonly string $platform,
10-
public readonly string $name,
11-
public readonly float $size,
12-
public readonly string $url,
13-
public private(set) ?string $md5,
14-
public readonly ?int $gogGameId,
15-
) {
16-
}
17-
18-
public function __unserialize(array $data): void
19-
{
20-
if (is_string($data['gogGameId'])) {
21-
$data['gogGameId'] = (int)$data['gogGameId'];
22-
}
23-
24-
foreach ($data as $key => $value) {
25-
$this->$key = $value;
26-
}
27-
}
28-
}
5+
class_alias(GameInstaller::class, 'App\DTO\DownloadDescription');

src/DTO/DownloadableItem.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\DTO;
4+
5+
interface DownloadableItem
6+
{
7+
public string $name {
8+
get;
9+
}
10+
11+
public string $url {
12+
get;
13+
}
14+
15+
public ?string $md5 {
16+
get;
17+
}
18+
19+
public ?int $gogGameId {
20+
get;
21+
}
22+
}

src/DTO/GameDetail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
final readonly class GameDetail
99
{
1010
/**
11-
* @param array<DownloadDescription> $downloads
11+
* @param array<GameInstaller> $downloads
1212
* @param array<GameExtra> $extras
1313
*/
1414
public function __construct(
1515
public int $id,
1616
public string $title,
1717
#[Deprecated]
1818
public string $cdKey,
19-
#[ArrayType(type: DownloadDescription::class)]
19+
#[ArrayType(type: GameInstaller::class)]
2020
public array $downloads,
2121
public string $slug,
2222
public array $extras,

src/DTO/GameExtra.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\DTO;
44

5-
final class GameExtra
5+
final class GameExtra implements DownloadableItem
66
{
77
public function __construct(
88
public readonly int $id,

src/DTO/GameInstaller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\DTO;
4+
5+
final class GameInstaller implements DownloadableItem
6+
{
7+
public function __construct(
8+
public readonly string $language,
9+
public readonly string $platform,
10+
public readonly string $name,
11+
public readonly float $size,
12+
public readonly string $url,
13+
public private(set) ?string $md5,
14+
public readonly ?int $gogGameId,
15+
) {
16+
}
17+
18+
public function __unserialize(array $data): void
19+
{
20+
if (is_string($data['gogGameId'])) {
21+
$data['gogGameId'] = (int)$data['gogGameId'];
22+
}
23+
24+
foreach ($data as $key => $value) {
25+
$this->$key = $value;
26+
}
27+
}
28+
}

src/Service/DownloadManager.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace App\Service;
44

5-
use App\DTO\DownloadDescription;
6-
use App\DTO\GameExtra;
5+
use App\DTO\DownloadableItem;
76
use Symfony\Component\HttpClient\Exception\ClientException;
87
use Symfony\Component\HttpFoundation\Request;
98
use Symfony\Component\HttpFoundation\Response;
@@ -21,7 +20,7 @@ public function __construct(
2120
}
2221

2322
public function getDownloadUrl(
24-
DownloadDescription|GameExtra $download
23+
DownloadableItem $download
2524
): string {
2625
if (str_starts_with($download->url, '/')) {
2726
return self::BASE_URL . $download->url;
@@ -30,8 +29,8 @@ public function getDownloadUrl(
3029
}
3130

3231
public function getFilename(
33-
DownloadDescription|GameExtra $download,
34-
int $httpTimeout = 3
32+
DownloadableItem $download,
33+
int $httpTimeout = 3
3534
): ?string {
3635
$url = $this->getRealDownloadUrl($download, $httpTimeout);
3736
if (!$url) {
@@ -42,11 +41,11 @@ public function getFilename(
4241
}
4342

4443
public function download(
45-
DownloadDescription|GameExtra $download,
46-
callable $callback,
47-
?int $startAt = null,
48-
int $httpTimeout = 3,
49-
array $curlOptions = [],
44+
DownloadableItem $download,
45+
callable $callback,
46+
?int $startAt = null,
47+
int $httpTimeout = 3,
48+
array $curlOptions = [],
5049
): ResponseStreamInterface {
5150
$url = $this->getRealDownloadUrl($download, $httpTimeout);
5251

@@ -72,8 +71,8 @@ public function download(
7271
}
7372

7473
private function getRealDownloadUrl(
75-
DownloadDescription|GameExtra $download,
76-
int $httpTimeout = 3
74+
DownloadableItem $download,
75+
int $httpTimeout = 3
7776
): ?string {
7877
if (str_starts_with($download->url, '/')) {
7978
$response = $this->httpClient->request(

src/Service/OwnedItemsManager.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace App\Service;
44

5-
use App\DTO\DownloadDescription;
5+
use App\DTO\DownloadableItem;
66
use App\DTO\GameDetail;
7-
use App\DTO\GameExtra;
87
use App\DTO\GameInfo;
98
use App\DTO\MovieInfo;
109
use App\DTO\OwnedItemInfo;
@@ -14,7 +13,6 @@
1413
use App\Enum\MediaType;
1514
use App\Enum\OperatingSystem;
1615
use App\Exception\AuthorizationException;
17-
use App\Helper\LatelyBoundStringValue;
1816
use App\Service\Persistence\PersistenceManager;
1917
use DateInterval;
2018
use Exception;
@@ -23,9 +21,7 @@
2321
use ReflectionException;
2422
use ReflectionProperty;
2523
use SimpleXMLElement;
26-
use Symfony\Component\HttpClient\Exception\ClientException;
2724
use Symfony\Component\HttpFoundation\Request;
28-
use Symfony\Component\HttpFoundation\Response;
2925
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
3026
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
3127
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
@@ -179,7 +175,7 @@ public function getLocalGameData(): array
179175
/**
180176
* @todo move this to deserialization (duplicate requests)
181177
*/
182-
public function getChecksum(DownloadDescription|GameExtra $download, GameDetail $game, int $httpTimeout = 3): ?string
178+
public function getChecksum(DownloadableItem $download, GameDetail $game, int $httpTimeout = 3): ?string
183179
{
184180
$parts = explode('/', $download->url);
185181
$id = $parts[array_key_last($parts)];
@@ -340,7 +336,7 @@ private function getMovieDetail(OwnedItemInfo $item, int $httpTimeout): GameDeta
340336
return $detail;
341337
}
342338

343-
private function setMd5(DownloadDescription|GameExtra $download, GameDetail $game, int $httpTimeout)
339+
private function setMd5(DownloadableItem $download, GameDetail $game, int $httpTimeout)
344340
{
345341
$md5 = $this->getChecksum($download, $game, $httpTimeout);
346342
if ($md5 === null) {

src/Service/Serializer/GameDetailNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Service\Serializer;
44

5-
use App\DTO\DownloadDescription;
5+
use App\DTO\GameInstaller;
66
use App\DTO\GameDetail;
77
use App\DTO\GameExtra;
88
use App\DTO\MultipleValuesWrapper;
@@ -14,7 +14,7 @@ public function normalize(array $value, array $context, Serializer $serializer):
1414
{
1515
if (!isset($value['downloads']['installers'])) {
1616
$finalDownloads = array_map(
17-
fn (array $download) => $serializer->deserialize($download, DownloadDescription::class),
17+
fn (array $download) => $serializer->deserialize($download, GameInstaller::class),
1818
$value['downloads'],
1919
);
2020
} else {
@@ -26,12 +26,12 @@ public function normalize(array $value, array $context, Serializer $serializer):
2626
$downloads = [];
2727
foreach ($sourceDownloads as $download) {
2828
$download['gogGameId'] = $context['id'];
29-
$downloads[] = $serializer->deserialize($download, DownloadDescription::class);
29+
$downloads[] = $serializer->deserialize($download, GameInstaller::class);
3030
}
3131
foreach ($value['expanded_dlcs'] ?? [] as $dlc) {
3232
foreach ($dlc['downloads']['installers'] as $download) {
3333
$download['gogGameId'] = $dlc['id'];
34-
$downloads[] = $serializer->deserialize($download, DownloadDescription::class);
34+
$downloads[] = $serializer->deserialize($download, GameInstaller::class);
3535
}
3636
}
3737

0 commit comments

Comments
 (0)