<?php

namespace App\Service;

use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\String\Slugger\SluggerInterface;

class PhotoUploader {
    public function __construct(
        private string $targetDirectory,
        private SluggerInterface $slugger,
    ){}

    public function upload(UploadedFile $file, string $galleryName) : string {
        $this->setTargetDirectory('photos/' . strtolower(str_replace(' ', '_', $galleryName)));
        $originalFilename = pathInfo($file->getClientOriginalName(), PATHINFO_FILENAME);
        $safeFilename = $this->slugger->slug($originalFilename);
        $filename = $safeFilename . '.' . $file->guessExtension();

        try {
            $file->move($this->getTargetDirectory(), $filename);
        } catch (FileException $e) {
            //todo
        }

        return $filename;
    }

    public function getTargetDirectory() : string {
        return $this->targetDirectory;
    }

    public function setTargetDirectory(string $directory) : void {
        $this->targetDirectory = $directory;
    }
}