<commit_before><?php
namespace SlaxWeb\Cache\Handler;

use SlaxWeb\Cache\AbstractHandler;
use SlaxWeb\Cache\Exception\WriteException;

/**
 * File Cache Handler
 *
 * The File Cache Handler writes the cache data to a file in the filesystem with
 * the cache data name as part of the file name.
 *
 * @package   SlaxWeb\Cache
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 (c) <NAME>
 * @license   MIT <https://opensource.org/licenses/MIT>
 * @link      https://github.com/slaxweb/
 * @version   0.1
 */
class File extends AbstractHandler
{
    /**
     * Filesystem location
     *
     * @var string
     */
    protected $path = "";

    /**
     * Class constructor
     *
     * The File Cache handler requires the path of the cache location, where the
     * handler will store the cache data to. The constructor simply stores this
     * path to the protected property. The constructor also checks if the handler
     * can write to that location, and throws an exception if that is not the case.
     *
     * @param string $path Filesystem location
     * @param int $maxAge Default maximum age in seconds, default 0
     *
     * @todo: check location permissions and throw exception
     */
    public function __construct(string $path, int $maxAge = 0)
    {
        $this->path = $path;
        $this->maxAge = $maxAge;
    }

    /**
     * @inheritDoc
     */
    public function write(string $name, string $data): AbstractHandler
    {
        return $this;
    }

    /**<commit_msg>add write to cache implementation<commit_after><?php
namespace SlaxWeb\Cache\Handler;

use SlaxWeb\Cache\AbstractHandler;
use SlaxWeb\Cache\Exception\WriteException;

/**
 * File Cache Handler
 *
 * The File Cache Handler writes the cache data to a file in the filesystem with
 * the cache data name as part of the file name.
 *
 * @package   SlaxWeb\Cache
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 (c) <NAME>
 * @license   MIT <https://opensource.org/licenses/MIT>
 * @link      https://github.com/slaxweb/
 * @version   0.1
 */
class File extends AbstractHandler
{
    /**
     * Filesystem location
     *
     * @var string
     */
    protected $path = "";

    /**
     * Class constructor
     *
     * The File Cache handler requires the path of the cache location, where the
     * handler will store the cache data to. The constructor simply stores this
     * path to the protected property. The constructor also checks if the handler
     * can write to that location, and throws an exception if that is not the case.
     *
     * @param string $path Filesystem location
     * @param int $maxAge Default maximum age in seconds, default 0
     *
     * @todo: check location permissions and throw exception
     */
    public function __construct(string $path, int $maxAge = 0)
    {
        $this->path = $path;
        $this->maxAge = $maxAge;
    }

    /**
     * @inheritDoc
     */
    public function write(string $name, string $data, int $maxAge = -1): AbstractHandler
    {
        if (file_put_contents(
                "{$this->path}{$name}.cache",
                serialize($this->prepData($data, $maxAge))
            ) === false
        ) {
            throw new WriteException("Error writting data to cache.");
        }
        return $this;
    }

    /**
{'fraction_non_alphanumeric': 0.10166919575113809, 'fraction_numerical': 0.005159332321699545, 'mean_word_length': 2.4261954261954264, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 4, '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': '1417687', 'n_tokens_mistral': 949, 'n_tokens_neox': 912, 'n_words': 336}