<commit_before><?php

namespace Gaufrette\FileStream;

use Gaufrette\FileStream;
use Gaufrette\StreamMode;

/**
 * Local stream
 *
 * @author <NAME> <<EMAIL>>
 */
class Local implements FileStream
{
    private $path;
    private $fileHandle;

    /**
     * Constructor
     *
     * @param  string $path
     */
    public function __construct($path)
    {
        $this->path = $path;
    }

    /**
     * {@inheritDoc}
     */
    public function open(StreamMode $mode)
    {
        $fileHandle = fopen($this->path, $mode->getMode());

        if (false === $fileHandle) {
            return false;
        }

        $this->mode = $mode;
        $this->fileHandle = $fileHandle;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function read($count)
    {
        return fread($this->fileHandle, $count);
    }

    /**
     * {@inheritDoc}
     */
    public function write($data)
    {
        return fwrite($this->fileHandle, $data);
    }

    /**
     * {@inheritDoc}
     */
    public function close()
    {
        $closed = fclose($this->fileHandle);

        if ($closed) {
            $this->mode = null;
            $this->fileHandle = null;
        }

        return $closed;
    }

    /**<commit_msg>Make the local adapter throw exception on invalid usage<commit_after><?php

namespace Gaufrette\FileStream;

use Gaufrette\FileStream;
use Gaufrette\StreamMode;

/**
 * Local stream
 *
 * @author <NAME> <<EMAIL>>
 */
class Local implements FileStream
{
    private $path;
    private $mode;
    private $fileHandle;

    /**
     * Constructor
     *
     * @param  string $path
     */
    public function __construct($path)
    {
        $this->path = $path;
    }

    /**
     * {@inheritDoc}
     */
    public function open(StreamMode $mode)
    {
        $fileHandle = fopen($this->path, $mode->getMode());

        if (false === $fileHandle) {
            return false;
        }

        $this->mode = $mode;
        $this->fileHandle = $fileHandle;

        return true;
    }

    /**
     * {@inheritDoc}
     */
    public function read($count)
    {
        if (false === $this->mode->allowsRead()) {
            throw new \LogicException('The stream does not allow read.');
        }

        return fread($this->fileHandle, $count);
    }

    /**
     * {@inheritDoc}
     */
    public function write($data)
    {
        if (false === $this->mode->allowsWrite()) {
            throw new \LogicException('The stream does not allow write.');
        }

        return fwrite($this->fileHandle, $data);
    }

    /**
     * {@inheritDoc}
     */
    public function close()
    {
        $closed = fclose($this->fileHandle);

        if ($closed) {
            $this->mode = null;
            $this->fileHandle = null;
        }

        return $closed;
    }

    /**
{'fraction_non_alphanumeric': 0.1474426807760141, 'fraction_numerical': 0.0, 'mean_word_length': 1.766829268292683, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '2628949', 'n_tokens_mistral': 879, 'n_tokens_neox': 850, 'n_words': 184}