src/Handler/FileHandler.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Handler;
  3. use Google\Cloud\Storage\StorageClient;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. class FileHandler
  6. {
  7. protected string $cdnKeyFilePath;
  8. protected string $cdnBucketName;
  9. protected string $cdnPublicUrl;
  10. protected RequestStack $requestStack;
  11. public function __construct(string $cdnKeyFilePath, string $cdnBucketName, string $cdnPublicUrl, RequestStack $requestStack)
  12. {
  13. $this->cdnKeyFilePath = $cdnKeyFilePath;
  14. $this->cdnBucketName = $cdnBucketName;
  15. $this->cdnPublicUrl = $cdnPublicUrl;
  16. $this->requestStack = $requestStack;
  17. }
  18. public function checkIfFileExistsInCdn(string $path): bool
  19. {
  20. $session = $this->requestStack->getSession();
  21. $cdnFiles = $session->get('cdnFiles', []);
  22. if (!isset($cdnFiles[$path])) {
  23. $storage = new StorageClient(['keyFilePath' => $this->cdnKeyFilePath]);
  24. $bucket = $storage->bucket($this->cdnBucketName);
  25. $file = $bucket->object($path);
  26. $cdnFiles[$path] = $file->exists();
  27. $session->set('cdnFiles', $cdnFiles);
  28. return $cdnFiles[$path];
  29. }
  30. return $cdnFiles[$path];
  31. }
  32. public function getAssetsBucketUrl(): string
  33. {
  34. return $this->cdnPublicUrl . $this->cdnBucketName;
  35. }
  36. }