<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2016, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ImageBundle\Archive;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Archiver
 */
class Archiver
{
    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @var string
     */
    private $cacheDir;

    /**
     * @var string
     */
    private $filenameSuffix;

    /**
     * @var string
     */
    private $uploadDir;

    /**
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack   Request stack
     * @param string                                         $cacheDir       Cache directory
     * @param string                                         $filenameSuffix Filename suffix
     * @param string                                         $uploadDir      Upload directory
     */
    public function __construct(RequestStack $requestStack, $cacheDir, $filenameSuffix, $uploadDir)
    {
        $this->requestStack = $requestStack;
        $this->cacheDir = $cacheDir;
        $this->filenameSuffix = $filenameSuffix;
        $this->uploadDir = $uploadDir;
    }

    /**
     * @return string
     */
    public function archive()
    {
        $this->prepareCacheDir();

        $filename = $this->buildFilename();
        $pathname = $this->buildPathname($filename);

        $this->buildZip($pathname);

        return $filename;
    }

    /**
     * @param string $filename Filename
     *
     * @return string
     */
    public function buildPathname($filename)
    {
        return implode(DIRECTORY_SEPARATOR, [
            $this->cacheDir,
            $filename,
        ]);
    }

    /**
     * @param string $pathname Pathname
     *
     * @throws \Darvin\ImageBundle\Archive\ArchiveException
     */
    private function buildZip($pathname)
    {
        $zip = new \ZipArchive();

        if (true !== $zip->open($pathname, \ZipArchive::CREATE)) {
            throw new ArchiveException(sprintf('Unable to create image archive "%s".', $pathname));
        }

        $finder = (new Finder())->in($this->uploadDir);

        foreach ($finder->directories() as $dir) {
            if (!$zip->addEmptyDir($dir->getRelativePathname())) {
                throw new ArchiveException(
                    sprintf('Unable to create directory "%s" in image archive "%s".', $dir->getRelativePathname(), $pathname)
                );
            }
        }
        foreach ($finder->files() as $file) {
            if (!$zip->addFile($file->getPathname(), $file->getRelativePathname())) {
                throw new ArchiveException(
                    sprintf('Unable to add file "%s" to image archive "%s".', $file->getPathname(), $pathname)
                );
            }
        }
        if (!$zip->close()) {
            throw new ArchiveException(sprintf('Unable to close image archive "%s".', $pathname));
        }
    }

    /**
     * @throws \Darvin\ImageBundle\Archive\ArchiveException
     */
    private function prepareCacheDir()
    {
        $fs = new Filesystem();

        if ($fs->exists($this->cacheDir)) {
            return;
        }
        try {
            $fs->mkdir($this->cacheDir);
        } catch (IOException $ex) {
            throw new ArchiveException(
                sprintf('Unable to create image archive cache dir "%s": "%s".', $this->cacheDir, $ex->getMessage())
            );<commit_msg>Clear cache directory in archiver.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2016, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ImageBundle\Archive;

use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Archiver
 */
class Archiver
{
    /**
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    private $requestStack;

    /**
     * @var string
     */
    private $cacheDir;

    /**
     * @var string
     */
    private $filenameSuffix;

    /**
     * @var string
     */
    private $uploadDir;

    /**
     * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack   Request stack
     * @param string                                         $cacheDir       Cache directory
     * @param string                                         $filenameSuffix Filename suffix
     * @param string                                         $uploadDir      Upload directory
     */
    public function __construct(RequestStack $requestStack, $cacheDir, $filenameSuffix, $uploadDir)
    {
        $this->requestStack = $requestStack;
        $this->cacheDir = $cacheDir;
        $this->filenameSuffix = $filenameSuffix;
        $this->uploadDir = $uploadDir;
    }

    /**
     * @return string
     */
    public function archive()
    {
        $this->prepareCacheDir();

        $filename = $this->buildFilename();
        $pathname = $this->buildPathname($filename);

        $this->buildZip($pathname);

        return $filename;
    }

    /**
     * @param string $filename Filename
     *
     * @return string
     */
    public function buildPathname($filename)
    {
        return implode(DIRECTORY_SEPARATOR, [
            $this->cacheDir,
            $filename,
        ]);
    }

    /**
     * @param string $pathname Pathname
     *
     * @throws \Darvin\ImageBundle\Archive\ArchiveException
     */
    private function buildZip($pathname)
    {
        $zip = new \ZipArchive();

        if (true !== $zip->open($pathname, \ZipArchive::CREATE)) {
            throw new ArchiveException(sprintf('Unable to create image archive "%s".', $pathname));
        }
        try {
            $finder = (new Finder())->in($this->uploadDir);
        } catch (\InvalidArgumentException $ex) {
            throw new ArchiveException(sprintf('Image upload directory "%s" does not exist.', $this->uploadDir));
        }
        foreach ($finder->directories() as $dir) {
            if (!$zip->addEmptyDir($dir->getRelativePathname())) {
                throw new ArchiveException(
                    sprintf('Unable to create directory "%s" in image archive "%s".', $dir->getRelativePathname(), $pathname)
                );
            }
        }
        foreach ($finder->files() as $file) {
            if (!$zip->addFile($file->getPathname(), $file->getRelativePathname())) {
                throw new ArchiveException(
                    sprintf('Unable to add file "%s" to image archive "%s".', $file->getPathname(), $pathname)
                );
            }
        }
        if (!$zip->close()) {
            throw new ArchiveException(sprintf('Unable to close image archive "%s".', $pathname));
        }
    }

    /**
     * @throws \Darvin\ImageBundle\Archive\ArchiveException
     */
    private function prepareCacheDir()
    {
        $fs = new Filesystem();

        if (!$fs->exists($this->cacheDir)) {
            try {
                $fs->mkdir($this->cacheDir);
            } catch (IOException $ex) {
                throw new ArchiveException(
                    sprintf('Unable to create image archive cache directory "%s": "%s".', $this->cacheDir, $ex->getMessage())
                );
            }
        }
        foreach ((new Finder())->in($this->cacheDir) as $file) {
            $fs->remove($file->getPathname());
{'fraction_non_alphanumeric': 0.1315390447308567, 'fraction_numerical': 0.0010108668182966893, 'mean_word_length': 1.9445684523809523, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 70, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6918015', 'n_tokens_mistral': 2153, 'n_tokens_neox': 2083, 'n_words': 485}