<?php
/**
 * @author Daniel Knudsen <d.knudsen@spawntree.de>
 * @date 16.02.24
 */


namespace App\Service;


use App\Entity\DocumentObject;
use App\Entity\MediaObject;
use Symfony\Component\HttpFoundation\RequestStack;
use Vich\UploaderBundle\Storage\StorageInterface;
use function Zenstruck\Foundry\instantiate;

class FileUrlService
{

    public function __construct(
        private RequestStack $requestStack,
        private StorageInterface $storage,
    ){}

    public function getFileUrl(?object $object)
    {
        if ($object !== null) {
            assert($object instanceof MediaObject || $object instanceof DocumentObject);
            $currentRequest = $this->requestStack->getCurrentRequest();
            if ($currentRequest !== null) {
                $apiUrl = $currentRequest->getSchemeAndHttpHost();
                $imageUri = $this->storage->resolveUri($object, 'file');
                return $apiUrl . $imageUri;
            }
        }
        return null;
    }
}