<commit_before><?php

namespace Curator\FSAccess;

trait WriteAdapterStreamWrapperTrait
{
  /**
   * @see ReadAdapterStreamWrapperTrait::failPath().
   *
   * @param string $path
   * @param string $read_write
   * @param string $operation_description
   * @param \ErrorException|NULL $error_exception
   */
  protected abstract function failPath($path, $read_write, $operation_description, \ErrorException $error_exception = NULL);

  /**
   * @return StreamContextWrapper
   */
  public abstract function getStreamContext();

  /**
   * Performs path transformations necessary for the stream wrapper to
   * function.
   *
   * For example, the FTP stream wrapper requires the username and password
   * to be included in the paths as part of the full ftp:// URI.
   *
   * The base implementation provided by ReadAdapterStreamWrapperTrait
   * does nothing.
   *
   * @param $path
   *   An absolute path to alter.
   * @return string
   *   The altered path.
   */
  protected abstract function alterPathForStreamWrapper($path);

  // TODO: WriteAdapterInterface exceptions contract
  public function filePutContents($filename, $data, $lock_if_able = TRUE) {
    $flags = ($lock_if_able && $this->getStreamContext()->getScheme() === 'file://'
      ? LOCK_EX : 0);

    try {
      return file_put_contents(
        $this->alterPathForStreamWrapper($filename),
        $data,
        $flags,
        $this->getStreamContext()->getContext()
      );
    } catch (\ErrorException $e) {
      throw new FileException($e->getMessage(), $filename, 0, $e);
    }
  }

  public function mkdir($path) {
    if (! mkdir(
      $this->alterPathForStreamWrapper($path),
      0755, // TODO: something fancier for mode, caution: umask
      FALSE,
      $this->getStreamContext()->getContext()
    )) {
      throw new \UnexpectedValueException();
    }
  }

  public function rename($old_name, $new_name) {
    $old_name = $this->alterPathForStreamWrapper($old_name);
    $new_name = $this->alterPathForStreamWrapper($new_name);
    try {<commit_msg>Use failPath() to report errors with WriteAdapterStreamWrapperTrait::filePutContents().<commit_after><?php

namespace Curator\FSAccess;

trait WriteAdapterStreamWrapperTrait
{
  /**
   * @see ReadAdapterStreamWrapperTrait::failPath().
   *
   * @param string $path
   * @param string $read_write
   * @param string $operation_description
   * @param \ErrorException|NULL $error_exception
   */
  protected abstract function failPath($path, $read_write, $operation_description, \ErrorException $error_exception = NULL);

  /**
   * @return StreamContextWrapper
   */
  public abstract function getStreamContext();

  /**
   * Performs path transformations necessary for the stream wrapper to
   * function.
   *
   * For example, the FTP stream wrapper requires the username and password
   * to be included in the paths as part of the full ftp:// URI.
   *
   * The base implementation provided by ReadAdapterStreamWrapperTrait
   * does nothing.
   *
   * @param $path
   *   An absolute path to alter.
   * @return string
   *   The altered path.
   */
  protected abstract function alterPathForStreamWrapper($path);

  // TODO: WriteAdapterInterface exceptions contract
  public function filePutContents($filename, $data, $lock_if_able = TRUE) {
    $flags = ($lock_if_able && $this->getStreamContext()->getScheme() === 'file://'
      ? LOCK_EX : 0);

    $filename = $this->alterPathForStreamWrapper($filename);
    try {
      return file_put_contents(
        $filename,
        $data,
        $flags,
        $this->getStreamContext()->getContext()
      );
    } catch (\ErrorException $e) {
      $this->failPath($filename, 'w', sprintf('Writing to file at %s', $filename), $e);
    }
  }

  public function mkdir($path) {
    if (! mkdir(
      $this->alterPathForStreamWrapper($path),
      0755, // TODO: something fancier for mode, caution: umask
      FALSE,
      $this->getStreamContext()->getContext()
    )) {
      throw new \UnexpectedValueException();
    }
  }

  public function rename($old_name, $new_name) {
    $old_name = $this->alterPathForStreamWrapper($old_name);
    $new_name = $this->alterPathForStreamWrapper($new_name);
    try {
{'fraction_non_alphanumeric': 0.11787072243346007, 'fraction_numerical': 0.0026140684410646386, 'mean_word_length': 3.384375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '5007059', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1203, 'n_words': 315}