Skip to content

Commit 9b141de

Browse files
committed
refactor: update request method to accept Method enum; simplify code
1 parent d305397 commit 9b141de

File tree

5 files changed

+14
-83
lines changed

5 files changed

+14
-83
lines changed

src/Module/Repository/Internal/GitHub/Api/Client.php

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

77
use Internal\DLoad\Module\Config\Schema\GitHub;
88
use Internal\DLoad\Module\HttpClient\Factory as HttpFactory;
9+
use Internal\DLoad\Module\HttpClient\Method;
910
use Internal\DLoad\Module\Repository\Internal\GitHub\Exception\GitHubRateLimitException;
1011
use Psr\Http\Client\ClientExceptionInterface;
1112
use Psr\Http\Client\ClientInterface;
@@ -41,12 +42,12 @@ public function __construct(
4142
}
4243

4344
/**
44-
* @param non-empty-string $method
45+
* @param Method|non-empty-string $method
4546
* @param array<string, string> $headers
4647
* @throws GitHubRateLimitException
4748
* @throws ClientExceptionInterface
4849
*/
49-
public function request(string $method, UriInterface $uri, array $headers = []): ResponseInterface
50+
public function request(Method|string $method, string|UriInterface $uri, array $headers = []): ResponseInterface
5051
{
5152
$request = $this->httpFactory->request($method, $uri, $headers + $this->defaultHeaders);
5253

src/Module/Repository/Internal/GitHub/Api/RepositoryApi.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Internal\DLoad\Module\Repository\Internal\GitHub\Api;
66

77
use Internal\DLoad\Module\HttpClient\Factory as HttpFactory;
8+
use Internal\DLoad\Module\HttpClient\Method;
89
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\Response\ReleaseInfo;
910
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\Response\RepositoryInfo;
1011
use Internal\DLoad\Module\Repository\Internal\GitHub\Exception\GitHubRateLimitException;
@@ -45,17 +46,13 @@ public function __construct(
4546
}
4647

4748
/**
48-
* @param non-empty-string $method
49+
* @param Method|non-empty-string $method
4950
* @param array<string, string> $headers
5051
* @throws GitHubRateLimitException
5152
* @throws ClientExceptionInterface
5253
*/
53-
public function request(string $method, string|UriInterface $uri, array $headers = []): ResponseInterface
54+
public function request(Method|string $method, string|UriInterface $uri, array $headers = []): ResponseInterface
5455
{
55-
if (\is_string($uri)) {
56-
$uri = $this->httpFactory->uri($uri);
57-
}
58-
5956
return $this->client->request($method, $uri, $headers);
6057
}
6158

@@ -65,10 +62,7 @@ public function request(string $method, string|UriInterface $uri, array $headers
6562
*/
6663
public function getRepository(): RepositoryInfo
6764
{
68-
$response = $this->client->request(
69-
'GET',
70-
$this->httpFactory->uri(\sprintf(self::URL_REPOSITORY, $this->repositoryPath)),
71-
);
65+
$response = $this->request(Method::Get, \sprintf(self::URL_REPOSITORY, $this->repositoryPath));
7266

7367
/** @var array{
7468
* name: string,
@@ -150,8 +144,8 @@ public function getReleases(int $page = 1): Paginator
150144
*/
151145
private function releasesRequest(int $page): ResponseInterface
152146
{
153-
return $this->client->request(
154-
'GET',
147+
return $this->request(
148+
Method::Get,
155149
$this->httpFactory->uri(
156150
\sprintf(self::URL_RELEASES, $this->repositoryPath),
157151
['page' => $page],

src/Module/Repository/Internal/GitHub/GitHubAsset.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Internal\DLoad\Module\Common\Architecture;
88
use Internal\DLoad\Module\Common\OperatingSystem;
9+
use Internal\DLoad\Module\HttpClient\Method;
910
use Internal\DLoad\Module\Repository\Internal\Asset;
1011
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\Response\AssetInfo;
1112
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\RepositoryApi;
@@ -58,7 +59,7 @@ public static function fromDTO(
5859
*/
5960
public function download(?\Closure $progress = null): \Generator
6061
{
61-
$response = $this->api->request('GET', $this->getUri());
62+
$response = $this->api->request(Method::Get, $this->getUri());
6263

6364
$body = $response->getBody();
6465
$size = $body->getSize();

src/Module/Repository/Internal/GitHub/GitHubRelease.php

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace Internal\DLoad\Module\Repository\Internal\GitHub;
66

7-
use Composer\Semver\VersionParser;
87
use Internal\DLoad\Module\Repository\Collection\AssetsCollection;
98
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\Response\ReleaseInfo;
109
use Internal\DLoad\Module\Repository\Internal\GitHub\Api\RepositoryApi;
1110
use Internal\DLoad\Module\Repository\Internal\Release;
1211
use Internal\DLoad\Module\Version\Version;
1312
use Internal\DLoad\Service\Destroyable;
14-
use Psr\Http\Client\ClientExceptionInterface;
1513

1614
/**
1715
* GitHub Release class representing a release in a GitHub repository.
@@ -25,7 +23,6 @@ final class GitHubRelease extends Release implements Destroyable
2523
* @param non-empty-string $name
2624
*/
2725
private function __construct(
28-
private readonly RepositoryApi $api,
2926
GitHubRepository $repository,
3027
string $name,
3128
Version $version,
@@ -38,9 +35,8 @@ public static function fromDTO(
3835
GitHubRepository $repository,
3936
ReleaseInfo $dto,
4037
): self {
41-
$name = self::getTagName($dto->name, $dto->tagName);
4238
$version = Version::fromVersionString($dto->tagName);
43-
$result = new self($api, $repository, $name, $version);
39+
$result = new self($repository, $dto->name, $version);
4440

4541
$result->assets = AssetsCollection::create(static function () use ($api, $result, $dto): \Generator {
4642
foreach ($dto->assets as $assetDTO) {
@@ -51,21 +47,6 @@ public static function fromDTO(
5147
return $result;
5248
}
5349

54-
/**
55-
* @throws ClientExceptionInterface
56-
*/
57-
public function getConfig(): string
58-
{
59-
$configUrl = \vsprintf('https://raw.githubusercontent.com/%s/%s/.rr.yaml', [
60-
$this->getRepository()->getName(),
61-
$this->getVersion(),
62-
]);
63-
64-
$response = $this->api->request('GET', $configUrl);
65-
66-
return $response->getBody()->__toString();
67-
}
68-
6950
public function destroy(): void
7051
{
7152
$this->assets === null or $this->assets->map(
@@ -74,24 +55,4 @@ public function destroy(): void
7455

7556
unset($this->assets, $this->repository);
7657
}
77-
78-
/**
79-
* Returns pretty-formatted tag (release) name.
80-
*
81-
* @note The return value is "pretty", but that does not mean that the tag physically exists.
82-
*/
83-
private static function getTagName(string $name, string $tagName): string
84-
{
85-
$parser = new VersionParser();
86-
87-
try {
88-
return $parser->normalize($tagName);
89-
} catch (\Throwable) {
90-
try {
91-
return $parser->normalize($name);
92-
} catch (\Throwable) {
93-
return 'dev-' . $tagName;
94-
}
95-
}
96-
}
9758
}

src/Module/Repository/Internal/Release.php

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

55
namespace Internal\DLoad\Module\Repository\Internal;
66

7-
use Composer\Semver\VersionParser;
87
use Internal\DLoad\Module\Repository\Collection\AssetsCollection;
98
use Internal\DLoad\Module\Repository\ReleaseInterface;
109
use Internal\DLoad\Module\Repository\Repository;
@@ -17,25 +16,17 @@
1716
*/
1817
abstract class Release implements ReleaseInterface
1918
{
20-
/**
21-
* Normalized release name.
22-
*
23-
* @var non-empty-string
24-
*/
25-
protected string $name;
26-
2719
protected AssetsCollection $assets;
2820

2921
/**
30-
* @param non-empty-string $name
22+
* @param non-empty-string $name Release name.
3123
*/
3224
public function __construct(
3325
protected Repository $repository,
34-
string $name,
26+
protected string $name,
3527
protected Version $version,
3628
iterable $assets = [],
3729
) {
38-
$this->name = $this->simplifyReleaseName($name);
3930
$this->assets = AssetsCollection::create($assets);
4031
}
4132

@@ -63,21 +54,4 @@ public function satisfies(Constraint $constraint): bool
6354
{
6455
return $constraint->isSatisfiedBy($this->version);
6556
}
66-
67-
/**
68-
* @param non-empty-string $name
69-
* @return non-empty-string
70-
*/
71-
private function simplifyReleaseName(string $name): string
72-
{
73-
$version = (new VersionParser())->normalize($name);
74-
75-
$parts = \explode('-', $version);
76-
$number = \substr($parts[0], 0, -2);
77-
78-
return isset($parts[1])
79-
? $number . '-' . $parts[1]
80-
: $number
81-
;
82-
}
8357
}

0 commit comments

Comments
 (0)