<commit_before><?php

namespace Pok\PoolDBM;

/**
 * Transaction
 *
 * @author <NAME> <<EMAIL>>
 */
class Transaction
{
    const QUEUE_ACTION_PERSIST = 0;
    const QUEUE_ACTION_REMOVE  = 1;

    /**
     * @var ModelManager
     */
    protected $manager;

    private $queueActions;
    private $queueManagerNames;
    private $queueModels;
    private $queueIds;

    /**
     * Constructor.
     *
     * @param ModelManager $manager
     */
    public function __construct(ModelManager $manager)
    {
        $this->manager = $manager;

        $this->cleanQueue();
    }

    /**
     * Starts a transaction on the underlying database connection.
     *
     * @return void
     */
    public function beginTransaction()
    {
        $this->launch(function ($manager) {
            $manager->beginTransaction();
        });
    }

    /**
     * Saves a model.
     *
     * @param object $model
     */
    public function persist($model)
    {
        $class    = $this->manager->getClassMetadata(get_class($model));
        $managers = $class->getFieldManagerNames();
        $pool     = $this->manager->getPool();
        $priority = $pool->getPriority('transaction');

        $id = null;

        if ($class->hasManagerReferenceGenerator()) {
            $managerName = $class->getManagerReferenceGenerator();
            $referenceModel = $model->{'get' . ucfirst($managerName)}();

            $pool->getManager($managerName)->persist($referenceModel);
            $id = $referenceModel->getId();

            unset($managers[$managerName]);
        }

        foreach ($managers as $key => $managerName) {
            if (isset($priority[$managerName])) {
                $this->doPersist($managerName, $model, $id);

                unset($managers[$key]); // dereference index
            }
        }

        foreach ($managers as $managerName) {
            $this->addQueue(self::QUEUE_ACTION_PERSIST, $managerName, $model, $id);
        }
    }

    /**
     * Deletes a model.
     *
     * @param object $model
     */
    public function remove($model)
    {
        $class    = $this->manager->getClassMetadata(get_class($model));
        $managers = $class->getFieldManagerNames();
        $priority = $this->manager->getPool()->getPriority('transaction');

        foreach ($managers as $key => $managerName) {
            if (isset($priority[$managerName])) {
                $this->manager->remove($model);

                unset($managers[$key]); // dereference index
            }
        }

        foreach ($managers as $managerName) {
            $this->addQueue(self::QUEUE_ACTION_REMOVE, $managerName, $model);
        }
    }

    /**
     * Commits a transaction on the underlying database connection.
     *
     * @return void
     */
    public function commit()
    {
        $this->launch(function ($manager) {
            $manager->commit();
        });

        $this->executeQueue();
    }

    /**
     * Performs a rollback on the underlying database connection.
     *
     * @return void
     */
    public function rollback()
    {
        $this->cleanQueue();

        $this->launch(function ($manager) {
            $manager->rollback();
        });
    }

    /**
     * Saves an model.
     *
     * @param string     $managerName
     * @param object     $model
     * @param null|mixed $id
     */
    protected function doPersist($managerName, $model, $id = null)<commit_msg>Fix flush queue in commit<commit_after><?php

namespace Pok\PoolDBM;

/**
 * Transaction
 *
 * @author <NAME> <<EMAIL>>
 */
class Transaction
{
    const QUEUE_ACTION_PERSIST = 0;
    const QUEUE_ACTION_REMOVE  = 1;

    /**
     * @var ModelManager
     */
    protected $manager;

    private $queueActions;
    private $queueManagerNames;
    private $queueModels;
    private $queueIds;

    /**
     * Constructor.
     *
     * @param ModelManager $manager
     */
    public function __construct(ModelManager $manager)
    {
        $this->manager = $manager;

        $this->cleanQueue();
    }

    /**
     * Starts a transaction on the underlying database connection.
     *
     * @return void
     */
    public function beginTransaction()
    {
        $this->launch(function ($manager) {
            $manager->beginTransaction();
        });
    }

    /**
     * Saves a model.
     *
     * @param object $model
     */
    public function persist($model)
    {
        $class    = $this->manager->getClassMetadata(get_class($model));
        $managers = $class->getFieldManagerNames();
        $pool     = $this->manager->getPool();
        $priority = $pool->getPriority('transaction');

        $id = null;

        if ($class->hasManagerReferenceGenerator()) {
            $managerName = $class->getManagerReferenceGenerator();
            $referenceModel = $model->{'get' . ucfirst($managerName)}();

            $pool->getManager($managerName)->persist($referenceModel);
            $id = $referenceModel->getId();

            unset($managers[$managerName]);
        }

        foreach ($managers as $key => $managerName) {
            if (isset($priority[$managerName])) {
                $this->doPersist($managerName, $model, $id);

                unset($managers[$key]); // dereference index
            }
        }

        foreach ($managers as $managerName) {
            $this->addQueue(self::QUEUE_ACTION_PERSIST, $managerName, $model, $id);
        }
    }

    /**
     * Deletes a model.
     *
     * @param object $model
     */
    public function remove($model)
    {
        $class    = $this->manager->getClassMetadata(get_class($model));
        $managers = $class->getFieldManagerNames();
        $priority = $this->manager->getPool()->getPriority('transaction');

        foreach ($managers as $key => $managerName) {
            if (isset($priority[$managerName])) {
                $this->manager->remove($model);

                unset($managers[$key]); // dereference index
            }
        }

        foreach ($managers as $managerName) {
            $this->addQueue(self::QUEUE_ACTION_REMOVE, $managerName, $model);
        }
    }

    /**
     * Commits a transaction on the underlying database connection.
     *
     * @return void
     */
    public function commit()
    {
        $this->launch(function ($manager) {
            $manager->commit();
        });

        $this->executeQueue();
        $this->manager->flush();
    }

    /**
     * Performs a rollback on the underlying database connection.
     *
     * @return void
     */
    public function rollback()
    {
        $this->cleanQueue();

        $this->launch(function ($manager) {
            $manager->rollback();
        });
    }

    /**
     * Saves an model.
     *
     * @param string     $managerName
     * @param object     $model
     * @param null|mixed $id
     */
    protected function doPersist($managerName, $model, $id = null)
{'fraction_non_alphanumeric': 0.14287780187997107, 'fraction_numerical': 0.0005784526391901663, 'mean_word_length': 1.9861830742659758, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 81, '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': '340373', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1910, 'n_words': 384}