<commit_before><?php
/**
 * This file is part of the Juvem package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace AppBundle\Cache;


class FileCache
{
    /**
     * Root directory containing cache files
     *
     * @var string
     */
    private $cacheDir;

    /**
     * FileCache constructor.
     *
     * @param string $cacheDir
     */
    public function __construct(string $cacheDir)
    {
        $this->cacheDir = $cacheDir;
    }

    /**
     * Determine if entry exists in cache
     *
     * @param FileCachePathGenerator $key Value to check
     * @return bool
     */
    public function contains(FileCachePathGenerator $key)
    {
        return file_exists($this->path($key->getPath()));
    }

    /**
     * Fetch element from cache
     *
     * @param FileCachePathGenerator $key Value
     * @return \SplFileInfo
     */
    public function fetch(FileCachePathGenerator $key)
    {
        if (!$this->contains($key)) {
            throw new \InvalidArgumentException('Cache miss');
        }
        return new \SplFileInfo($this->path($key->getPath()));
    }

    /**
     * Save into cache
     *
     * @param FileCachePathGenerator       $key   Key
     * @param resource|string|\SplFileInfo $value Data
     * @return bool|int
     */
    public function save(FileCachePathGenerator $key, $value)
    {
        if (is_resource($value)) {
            return $this->saveResource($key, $value);
        } elseif (is_string($value)) {
            return $this->saveString($key, $value);
        } elseif ($value instanceof \SplFileInfo) {
            return $this->saveFileInfo($key, $value);
        }
        throw new \InvalidArgumentException('Value has unknown type and can not be handled');
    }

    /**
     * Save file
     *
     * @param FileCachePathGenerator $key   Key
     * @param \SplFileInfo           $value Data File
     * @return bool
     */
    public function saveFileInfo(FileCachePathGenerator $key, \SplFileInfo $value)
    {
        $this->ensureDirectoryExists($key);
        $source = $value->getPathname();
        $target = $this->path($key->getPath());
        return copy($source, $target);
    }

    /**
     * Save file resource
     *
     * @param FileCachePathGenerator $key   Key
     * @param resource               $value Data resource
     * @return bool
     */
    public function saveResource(FileCachePathGenerator $key, resource $value)
    {
        $this->ensureDirectoryExists($key);
        $target = fopen($this->path($key), 'w');
        while (!feof($value)) {<commit_msg>Fixed handling of resources in file cache<commit_after><?php
/**
 * This file is part of the Juvem package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */


namespace AppBundle\Cache;


class FileCache
{
    /**
     * Root directory containing cache files
     *
     * @var string
     */
    private $cacheDir;

    /**
     * FileCache constructor.
     *
     * @param string $cacheDir
     */
    public function __construct(string $cacheDir)
    {
        $this->cacheDir = $cacheDir;
    }

    /**
     * Determine if entry exists in cache
     *
     * @param FileCachePathGenerator $key Value to check
     * @return bool
     */
    public function contains(FileCachePathGenerator $key)
    {
        return file_exists($this->path($key->getPath()));
    }

    /**
     * Fetch element from cache
     *
     * @param FileCachePathGenerator $key Value
     * @return \SplFileInfo
     */
    public function fetch(FileCachePathGenerator $key)
    {
        if (!$this->contains($key)) {
            throw new \InvalidArgumentException('Cache miss');
        }
        return new \SplFileInfo($this->path($key->getPath()));
    }

    /**
     * Save into cache
     *
     * @param FileCachePathGenerator       $key   Key
     * @param resource|string|\SplFileInfo $value Data
     * @return bool|int
     */
    public function save(FileCachePathGenerator $key, $value)
    {
        if (is_resource($value)) {
            return $this->saveResource($key, $value);
        } elseif (is_string($value)) {
            return $this->saveString($key, $value);
        } elseif ($value instanceof \SplFileInfo) {
            return $this->saveFileInfo($key, $value);
        }
        throw new \InvalidArgumentException('Value has unknown type and can not be handled');
    }

    /**
     * Save file
     *
     * @param FileCachePathGenerator $key   Key
     * @param \SplFileInfo           $value Data File
     * @return bool
     */
    public function saveFileInfo(FileCachePathGenerator $key, \SplFileInfo $value)
    {
        $this->ensureDirectoryExists($key);
        $source = $value->getPathname();
        $target = $this->path($key->getPath());
        return copy($source, $target);
    }

    /**
     * Save file resource
     *
     * @param FileCachePathGenerator $key   Key
     * @param resource               $value Data resource
     * @return bool
     */
    public function saveResource(FileCachePathGenerator $key, $value)
    {
        if (!is_resource($value)) {
            throw new \InvalidArgumentException('A resource must be transmitted');
        }
        $this->ensureDirectoryExists($key);
        $target = fopen($this->path($key), 'w');
        while (!feof($value)) {
{'fraction_non_alphanumeric': 0.12133285041651576, 'fraction_numerical': 0.0, 'mean_word_length': 2.229824561403509, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3847755', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1523, 'n_words': 408}