Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Symfony HttpClient #281

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class SwagSecurityChecker implements HealthCheckerInterface, CheckerInterface
{
Expand All @@ -24,6 +25,8 @@ public function __construct(
#[Autowire('%kernel.shopware_version%')]
private readonly string $shopwareVersion,
private readonly CacheInterface $cacheObject,
#[Autowire(lazy: true)]
private readonly HttpClientInterface $httpClient,
) {}

public function collect(HealthCollection $collection): void
Expand Down Expand Up @@ -57,8 +60,9 @@ private function hasSecurityAdvisories(): bool
$cacheKey = \sprintf('security-advisories-%s', $this->shopwareVersion);

return $this->cacheObject->get($cacheKey, function (ItemInterface $cacheItem) {
$securityJson = file_get_contents('https://raw.githubusercontent.com/FriendsOfShopware/shopware-static-data/main/data/security.json');
if ($securityJson === false) {
try {
$securityJson = $this->httpClient->request('GET', 'https://raw.githubusercontent.com/FriendsOfShopware/shopware-static-data/main/data/security.json')->getContent();
} catch (\Throwable) {
throw new \RuntimeException('Could not fetch security.json');
}

Expand Down Expand Up @@ -212,8 +216,9 @@ private function getReleasesSupport(): array
$cacheKey = \sprintf('shopware-releases-support-%s', $this->shopwareVersion);

return $this->cacheObject->get($cacheKey, function (ItemInterface $cacheItem) {
$releasesJson = file_get_contents('https://raw.githubusercontent.com/shopware/shopware/trunk/releases.json');
if ($releasesJson === false) {
try {
$releasesJson = $this->httpClient->request('GET', 'https://raw.githubusercontent.com/shopware/shopware/trunk/releases.json')->getContent();
} catch (\Throwable) {
throw new \RuntimeException('Could not fetch releases.json');
}

Expand Down
9 changes: 6 additions & 3 deletions src/Controller/ShopwareFilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AutoconfigureTag('monolog.logger', ['channel' => 'frosh-tools'])]
#[Route(path: '/api/_action/frosh-tools', defaults: ['_routeScope' => ['api'], '_acl' => ['frosh_tools:read']])]
Expand All @@ -46,6 +47,8 @@ public function __construct(
private readonly LoggerInterface $froshToolsLogger,
private readonly EntityRepository $userRepository,
private readonly EntityRepository $integrationRepository,
#[Autowire(lazy: true)]
private readonly HttpClientInterface $httpClient,
) {
$this->isPlatform = !is_dir($this->projectDir . '/vendor/shopware/core') && is_dir($this->projectDir . '/src/Core');
}
Expand All @@ -58,8 +61,8 @@ public function listShopwareFiles(): JsonResponse
}

$url = sprintf('https://swagger.docs.fos.gg/version/%s/Files.xxhsums', $this->shopwareVersion);

$data = trim((string) @file_get_contents($url));
$data = $this->httpClient->request('GET', $url)->getContent(false);
$data = trim((string) $data);

if (empty($data)) {
return new JsonResponse(['error' => 'No file information for this Shopware version']);
Expand Down Expand Up @@ -194,7 +197,7 @@ private function getShopwareUrl(string $name): string

private function getOriginalFileContent(string $name): ?string
{
return @file_get_contents($this->getShopwareUrl($name) . '?raw=true') ?: null;
return $this->httpClient->request('GET', $this->getShopwareUrl($name) . '?raw=true')->getContent(false) ?: null;
}

private function isIgnoredFileHash(string $file): int
Expand Down