<?php

namespace App\Service\File\Namer;

use RandomLib;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * This namer keeps the original filename but appends a random string to the end.
 */
class OriginalRandomNamer implements NamerInterface
{
    private $opts = [
        'length' => 6,
    ];

    /**
     * @param int $length of random portion of filename
     */
    public function setLength($length)
    {
        $this->opts['length'] = (int) $length;
    }

    public function getName(UploadedFile $file)
    {
        $factory = new RandomLib\Factory();
        $generator = $factory->getMediumStrengthGenerator();

        return strtolower(sprintf(
            '%s_%s.%s',
            pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME),
            $generator->generateString($this->opts['length'], RandomLib\Generator::CHAR_ALNUM),
            $file->getClientOriginalExtension()
        ));
    }
}
