<?php

namespace App\Service;

use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Psr\Log\LoggerInterface;

class FileUploader
{
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function upload($uploadDir, $file, $filename)
    {
        try {
            $file->move($uploadDir, $filename);
        } catch (FileException $e) {
            $this->logger->error("failed to upload image: " . $e->getMessage());
            throw new FileException("Failed to upload file");
        }
    }

    public function uploadExamples($examplesDir, $file)
    {
        try {
            $file->move($examplesDir, urlencode($file->getClientOriginalName()));
        } catch (FileException $e) {
            $this->logger->error("failed to upload image: " . $e->getMessage());
            throw new FileException("Failed to upload image file");
        }
    }

    public function uploadAvatar($avatarsDir, $file)
    {
        try {
            $file->move($avatarsDir, $file->getClientOriginalName());
        } catch (FileException $e) {
            $this->logger->error("failed to upload image: " . $e->getMessage());
            throw new FileException("Failed to upload image file");
        }
    }
}
