<commit_before><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 07/05/2014
 * Time: 10:33
 */

namespace Packaged\Mappers;

use Doctrine\ORM\Tools\SchemaTool;
use Packaged\Mappers\Exceptions\InvalidLoadException;

/**
 * Class DoctrineMapper
 * @package Packaged\Mappers
 * @MappedSuperclass
 * @HasLifecycleCallbacks
 */
abstract class DoctrineMapper extends BaseMapper
{
  /**
   * @return \Doctrine\ORM\EntityManager
   */
  public static function getEntityManager()
  {
    return static::getConnectionResolver()->getConnection(
      static::getServiceName()
    );
  }

  /**
   * @param mixed $id
   *
   * @return static
   * @throws \Doctrine\ORM\TransactionRequiredException
   * @throws \Doctrine\ORM\ORMException
   * @throws \Doctrine\ORM\OptimisticLockException
   * @throws \Doctrine\ORM\ORMInvalidArgumentException
   * @throws InvalidLoadException
   */
  public static function load($id)
  {
    if($id === null)
    {
      throw new InvalidLoadException('No ID passed to load');
    }
    else
    {
      $obj = static::getEntityManager()->find(get_called_class(), $id);
      if(!$obj)
      {
        throw new InvalidLoadException('No object found with that ID');
      }
      return $obj->setExists(true);
    }
  }

  /**
   * @param array $criteria
   * @param null  $order
   * @param null  $limit
   * @param null  $offset
   *
   * @return static[]
   */
  public static function loadWhere(
    array $criteria, $order = null, $limit = null, $offset = null
  )
  {
    return static::getEntityManager()->getRepository(get_called_class())
      ->findBy($criteria, $order, $limit, $offset);
  }

  public static function loadFromMaster($id = null)
  {
    //TODO: use Master EntityManager
    static::load($id);
  }

  public function save()
  {
    $em = static::getEntityManager();
    $em->persist($this);
    $em->flush();
    $this->setExists(true);
  }

  /**
   * @PrePersist
   */
  public function preCreate()
  {
    parent::preCreate();
  }

  /**
   * @PreUpdate
   */
  public function preUpdate()
  {
    parent::preUpdate();
  }

  /**
   * @PostLoad
   */
  public function postLoad()
  {
    $this->setExists(true);
  }

  /**
   * @param $newKey
   *
   * @return static
   */
  public function saveAsNew($newKey = null)
  {
    // timestamps
    $new = new static();
    foreach($this->_getColumnMap() as $field)
    {
      $new->$field = $this->$field;
    }

    if(!$this->isCompositeId())
    {
      $keys = $this->_getKeys();
      $new->hydrate([reset($keys) => $newKey]);
    }
    $new->save();
    return $new;
  }

  public function reload()
  {
    static::getEntityManager()->refresh($this);
    return $this;
  }

  public function delete()
  {
    static::getEntityManager()->remove($this);
    static::getEntityManager()->flush($this);
  }

  protected static function _getMetadata()
  {
    return static::getEntityManager()->getClassMetadata(get_called_class());
  }

  /**
   * @return bool
   */
  public function isCompositeId()
  {
    return count($this->id()) > 1;
  }

  public function increment($field, $count)
  {
    $em       = static::getEntityManager();
    $keys     = $this->_getKeyValues();
    $keyArray = [];<commit_msg>DoctrineMapper::delete now sets exists to false and returns $this<commit_after><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 07/05/2014
 * Time: 10:33
 */

namespace Packaged\Mappers;

use Doctrine\ORM\Tools\SchemaTool;
use Packaged\Mappers\Exceptions\InvalidLoadException;

/**
 * Class DoctrineMapper
 * @package Packaged\Mappers
 * @MappedSuperclass
 * @HasLifecycleCallbacks
 */
abstract class DoctrineMapper extends BaseMapper
{
  /**
   * @return \Doctrine\ORM\EntityManager
   */
  public static function getEntityManager()
  {
    return static::getConnectionResolver()->getConnection(
      static::getServiceName()
    );
  }

  /**
   * @param mixed $id
   *
   * @return static
   * @throws \Doctrine\ORM\TransactionRequiredException
   * @throws \Doctrine\ORM\ORMException
   * @throws \Doctrine\ORM\OptimisticLockException
   * @throws \Doctrine\ORM\ORMInvalidArgumentException
   * @throws InvalidLoadException
   */
  public static function load($id)
  {
    if($id === null)
    {
      throw new InvalidLoadException('No ID passed to load');
    }
    else
    {
      $obj = static::getEntityManager()->find(get_called_class(), $id);
      if(!$obj)
      {
        throw new InvalidLoadException('No object found with that ID');
      }
      return $obj->setExists(true);
    }
  }

  /**
   * @param array $criteria
   * @param null  $order
   * @param null  $limit
   * @param null  $offset
   *
   * @return static[]
   */
  public static function loadWhere(
    array $criteria, $order = null, $limit = null, $offset = null
  )
  {
    return static::getEntityManager()->getRepository(get_called_class())
      ->findBy($criteria, $order, $limit, $offset);
  }

  public static function loadFromMaster($id = null)
  {
    //TODO: use Master EntityManager
    static::load($id);
  }

  public function save()
  {
    $em = static::getEntityManager();
    $em->persist($this);
    $em->flush();
    $this->setExists(true);
  }

  /**
   * @PrePersist
   */
  public function preCreate()
  {
    parent::preCreate();
  }

  /**
   * @PreUpdate
   */
  public function preUpdate()
  {
    parent::preUpdate();
  }

  /**
   * @PostLoad
   */
  public function postLoad()
  {
    $this->setExists(true);
  }

  /**
   * @param $newKey
   *
   * @return static
   */
  public function saveAsNew($newKey = null)
  {
    // timestamps
    $new = new static();
    foreach($this->_getColumnMap() as $field)
    {
      $new->$field = $this->$field;
    }

    if(!$this->isCompositeId())
    {
      $keys = $this->_getKeys();
      $new->hydrate([reset($keys) => $newKey]);
    }
    $new->save();
    return $new;
  }

  public function reload()
  {
    static::getEntityManager()->refresh($this);
    return $this;
  }

  public function delete()
  {
    static::getEntityManager()->remove($this);
    static::getEntityManager()->flush($this);
    $this->setExists(false);
    return $this;
  }

  protected static function _getMetadata()
  {
    return static::getEntityManager()->getClassMetadata(get_called_class());
  }

  /**
   * @return bool
   */
  public function isCompositeId()
  {
    return count($this->id()) > 1;
  }

  public function increment($field, $count)
  {
    $em       = static::getEntityManager();
    $keys     = $this->_getKeyValues();
    $keyArray = [];
{'fraction_non_alphanumeric': 0.15352824123679779, 'fraction_numerical': 0.0039797948874942595, 'mean_word_length': 2.850324101355333, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 54, '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': '2605932', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2103, 'n_words': 417}