<commit_before><?php

namespace CL\Bundle\ArchiverBundle\Archiver\Entity;

use Doctrine\ORM\EntityManager;

/**
 * Class that handles the archiving and unarchiving of entities
 */
class EntityArchiver
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var array
     */
    protected $entityArchived = array();

    /**
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @param ArchivableEntityInterface $entity         The entity that is archivable
     * @param string                    $archivedEntity Classname of the entity that will be used to archive the entity
     *                                                  (must implement ExtractableEntityInterface)
     *
     * @throws \InvalidArgumentException
     */
    public function addArchivable(ArchivableEntityInterface $entity, $archivedEntity)
    {
        $implements      = class_implements($entity);
        $shouldImplement = 'CL\Bundle\ArchiverBundle\Archiver\Entity\ArchivableEntityInterface';
        if (!in_array($shouldImplement, $implements)) {
            throw new \InvalidArgumentException(sprintf('Archivable entity should implement %s', $shouldImplement));
        }

        $implements      = class_implements($archivedEntity);
        $shouldImplement = 'CL\Bundle\ArchiverBundle\Archiver\Entity\ExtractableEntityInterface';
        if (!in_array($shouldImplement, $implements)) {
            throw new \InvalidArgumentException(sprintf('Archived entity should implement %s', $shouldImplement));
        }

        $this->entityArchived[get_class($entity)] = $archivedEntity;
    }

    /**
     * Archives the given entity by copying it's data into another entity that implements ExtractableEntityInterface
     *
     * @param ArchivableEntityInterface $entity         The entity to be archived
     * @param bool                      $flush          Whether the archived entity should be flushed to
     *                                                  the database automatically
     * @param bool                      $removeOriginal Whether the original entity should be removed after
     *                                                  successful archiving (requires $persistAndFlush to be true)
     *
     * @return ExtractableEntityInterface
     *
     * @throws \InvalidArgumentException If an entity was given that has not been persisted to the database yet
     */
    public function archive(ArchivableEntityInterface $entity, $flush = true, $removeOriginal = false)
    {
        $data     = [];
        $metadata = $this->em->getClassMetadata(get_class($entity));
        foreach ($metadata->getFieldNames() as $field) {
            if (!in_array($field, ['id'])) {
                $data[$field] = $metadata->getFieldValue($entity, $field);
            }
        }

        $id = $entity->getId();
        if (empty($id)) {
            throw new \InvalidArgumentException('Can\'t archive an entity without an ID, are you sure it has been persisted already?');
        }

        $archivedEntity = $this->createExtractableEntity($entity);
        $archivedEntity->setOriginalId($entity->getId());
        $archivedEntity->setData($data);

        $this->em->persist($archivedEntity);

        if ($removeOriginal === true) {
            $this->em->remove($entity);
        }
        if ($flush === true) {
            $this->em->flush();
        }

        return $archivedEntity;
    }

    /**
     * @param ExtractableEntityInterface $archivedEntity The entity that was used to archive another entity.
     * @param bool                       $create         Whether a new entity should be constructed if the
     *                                                   original entity no longer exists.
     * @param bool                       $removeArchive  Whether the archived entity should be removed after extraction.
     *
     * @return ExtractableEntityInterface The (original) unarchived entity.
     *
     * @throws \LogicException If the original entity could not be found and $create is false
     */
    public function extract(ExtractableEntityInterface $archivedEntity, $create = true, $removeArchive = true)<commit_msg>fixed line deemed too wide<commit_after><?php

namespace CL\Bundle\ArchiverBundle\Archiver\Entity;

use Doctrine\ORM\EntityManager;

/**
 * Class that handles the archiving and unarchiving of entities
 */
class EntityArchiver
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * @var array
     */
    protected $entityArchived = array();

    /**
     * @param EntityManager $em
     *
     * @throws \LogicException
     */
    public function __construct(EntityManager $em)
    {
        if ($em->isOpen() !== true) {
            throw new \LogicException('The entity manager\'s connection must be open for the archiver to work');
        }

        $this->em = $em;
    }

    /**
     * @param ArchivableEntityInterface $entity         The entity that is archivable
     * @param string                    $archivedEntity Classname of the entity that will be used to archive the entity
     *                                                  (must implement ExtractableEntityInterface)
     *
     * @throws \InvalidArgumentException
     */
    public function addArchivable(ArchivableEntityInterface $entity, $archivedEntity)
    {
        $implements      = class_implements($entity);
        $shouldImplement = 'CL\Bundle\ArchiverBundle\Archiver\Entity\ArchivableEntityInterface';
        if (!in_array($shouldImplement, $implements)) {
            throw new \InvalidArgumentException(sprintf('Archivable entity should implement %s', $shouldImplement));
        }

        $implements      = class_implements($archivedEntity);
        $shouldImplement = 'CL\Bundle\ArchiverBundle\Archiver\Entity\ExtractableEntityInterface';
        if (!in_array($shouldImplement, $implements)) {
            throw new \InvalidArgumentException(sprintf('Archived entity should implement %s', $shouldImplement));
        }

        $this->entityArchived[get_class($entity)] = $archivedEntity;
    }

    /**
     * Archives the given entity by copying it's data into another entity that implements ExtractableEntityInterface
     *
     * @param ArchivableEntityInterface $entity         The entity to be archived
     * @param bool                      $flush          Whether the archived entity should be flushed to
     *                                                  the database automatically
     * @param bool                      $removeOriginal Whether the original entity should be removed after
     *                                                  successful archiving (requires $persistAndFlush to be true)
     *
     * @return ExtractableEntityInterface
     *
     * @throws \InvalidArgumentException If an entity was given that has not been persisted to the database yet
     */
    public function archive(ArchivableEntityInterface $entity, $flush = true, $removeOriginal = false)
    {
        $data     = [];
        $metadata = $this->em->getClassMetadata(get_class($entity));
        foreach ($metadata->getFieldNames() as $field) {
            if (!in_array($field, ['id'])) {
                $data[$field] = $metadata->getFieldValue($entity, $field);
            }
        }

        $id = $entity->getId();
        if (empty($id)) {
            throw new \InvalidArgumentException(
                'Cannot archive an entity without an ID, are you sure it has been persisted already?'
            );
        }

        $archivedEntity = $this->createExtractableEntity($entity);
        $archivedEntity->setOriginalId($entity->getId());
        $archivedEntity->setData($data);

        $this->em->persist($archivedEntity);

        if ($removeOriginal === true) {
            $this->em->remove($entity);
        }
        if ($flush === true) {
            $this->em->flush();
        }

        return $archivedEntity;
    }

    /**
     * @param ExtractableEntityInterface $archivedEntity The entity that was used to archive another entity.
     * @param bool                       $create         Whether a new entity should be constructed if the
     *                                                   original entity no longer exists.
     * @param bool                       $removeArchive  Whether the archived entity should be removed after extraction.
     *
     * @return ExtractableEntityInterface The (original) unarchived entity.
     *
     * @throws \LogicException If the original entity could not be found and $create is false
     */
    public function extract(ExtractableEntityInterface $archivedEntity, $create = true, $removeArchive = true)
{'fraction_non_alphanumeric': 0.09336217693271091, 'fraction_numerical': 0.0, 'mean_word_length': 2.1405076868072936, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1988545', 'n_tokens_mistral': 2141, 'n_tokens_neox': 2071, 'n_words': 618}