<?php
namespace App\Handler;
use Google\Cloud\Storage\StorageClient;
use Symfony\Component\HttpFoundation\RequestStack;
class FileHandler
{
protected string $cdnKeyFilePath;
protected string $cdnBucketName;
protected string $cdnPublicUrl;
protected RequestStack $requestStack;
public function __construct(string $cdnKeyFilePath, string $cdnBucketName, string $cdnPublicUrl, RequestStack $requestStack)
{
$this->cdnKeyFilePath = $cdnKeyFilePath;
$this->cdnBucketName = $cdnBucketName;
$this->cdnPublicUrl = $cdnPublicUrl;
$this->requestStack = $requestStack;
}
public function checkIfFileExistsInCdn(string $path): bool
{
$session = $this->requestStack->getSession();
$cdnFiles = $session->get('cdnFiles', []);
if (!isset($cdnFiles[$path])) {
$storage = new StorageClient(['keyFilePath' => $this->cdnKeyFilePath]);
$bucket = $storage->bucket($this->cdnBucketName);
$file = $bucket->object($path);
$cdnFiles[$path] = $file->exists();
$session->set('cdnFiles', $cdnFiles);
return $cdnFiles[$path];
}
return $cdnFiles[$path];
}
public function getAssetsBucketUrl(): string
{
return $this->cdnPublicUrl . $this->cdnBucketName;
}
}