<?php

/*
 * This file was created by developers working at BitBag
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Downloader;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Webmozart\Assert\Assert;

final class ImageDownloader implements ImageDownloaderInterface
{
    public function __construct(private Filesystem $filesystem)
    {
    }

    public function download(string $url): UploadedFile
    {
        $path = rtrim(sys_get_temp_dir(), \DIRECTORY_SEPARATOR) . \DIRECTORY_SEPARATOR . md5(random_bytes(10));
        $pathInfo = pathinfo($url);
        $extension = $pathInfo['extension'] ?? null;
        $originalName = $pathInfo['basename'];

        if (null !== $extension) {
            $path .= '.' . $extension;
        }
        $contents = file_get_contents($url);
        Assert::string($contents, sprintf('Content of file in path %s is null', $path));
        $this->filesystem->dumpFile($path, $contents);

        return new UploadedFile($path, $originalName);
    }
}
