<commit_before><?php

namespace Oro\Bundle\IntegrationBundle\Entity\Repository;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use JMS\JobQueueBundle\Entity\Job;
use Oro\Bundle\BatchBundle\ORM\Query\BufferedQueryResultIterator;
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\IntegrationBundle\Entity\Status;

class ChannelRepository extends EntityRepository
{
    const BUFFER_SIZE = 100;

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @info Check if task is running
     *
     * @param string   $commandName
     * @param int|null $integrationId
     *
     * @return int
     */
    public function getRunningSyncJobsCount($commandName, $integrationId = null)
    {
        $qb = $this->getSyncJobsCountQueryBuilder($commandName, $integrationId);
        $qb->andWhere('j.state=:stateName');
        $qb->setParameter('stateName', Job::STATE_RUNNING);

        return (int)$qb->getQuery()->getSingleScalarResult();
    }

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @info Check if task is pending or running
     *
     * @param string   $commandName
     * @param int|null $integrationId
     *
     * @return int
     */
    public function getExistingSyncJobsCount($commandName, $integrationId = null)
    {
        $qb = $this->getSyncJobsCountQueryBuilder($commandName, $integrationId);
        $qb->andWhere($qb->expr()->in('j.state', ':states'));
        $qb->setParameter('states', [Job::STATE_RUNNING, Job::STATE_PENDING, Job::STATE_NEW]);

        return (int)$qb->getQuery()->getSingleScalarResult();
    }

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @param string   $commandName
     * @param int|null $integrationId
     *
     * @return QueryBuilder
     */
    protected function getSyncJobsCountQueryBuilder($commandName, $integrationId = null)
    {
        /** @var QueryBuilder $qb */
        $qb = $this->getEntityManager()
            ->getRepository('JMSJobQueueBundle:Job')
            ->createQueryBuilder('j')
            ->select('count(j.id)')
            ->andWhere('j.command=:commandName')
            ->setParameter('commandName', $commandName);

        if ($integrationId) {
            $qb->andWhere(
                $qb->expr()->orX(
                    $qb->expr()->like('cast(j.args as text)', ':integrationIdType1'),
                    $qb->expr()->like('cast(j.args as text)', ':integrationIdType2'),
                    $qb->expr()->andX(
                        $qb->expr()->notLike('cast(j.args as text)', ':noIntegrationIdType1'),
                        $qb->expr()->notLike('cast(j.args as text)', ':noIntegrationIdType2')
                    )
                )
            )
                ->setParameter('integrationIdType1', '%--integration-id=' . $integrationId . '%')
                ->setParameter('noIntegrationIdType1', '%--integration-id=%')
                ->setParameter('integrationIdType2', '%-i=' . $integrationId . '%')
                ->setParameter('noIntegrationIdType2', '%-i=%');
        }

        return $qb;
    }

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @param string[]|string      $commandName
     * @param string[]|string|null $arguments
     * @param string[]|string|null $states
     *
     * @return QueryBuilder
     */
    protected function getQBSyncJobs($commandName, $arguments = null, $states = null)
    {
        /** @var QueryBuilder $qb */
        $qb = $this->getEntityManager()
            ->getRepository('JMSJobQueueBundle:Job')
            ->createQueryBuilder('j');

        if (is_array($commandName)) {
            $qb->andWhere('j.command in (:commandName)');
        } else {
            $qb->andWhere('j.command=:commandName');
        }

        if (!empty($states)) {
            if (is_array($states)) {
                $qb->andWhere('j.state in (:stateName)');
            } else {
                $qb->andWhere('j.state=:stateName');
            }
        }

        $qb->setParameter('stateName', $states)
            ->setParameter('commandName', $commandName);

        if (!empty($arguments)) {
            if (is_array($arguments)) {
                $orX = $qb->expr()->orX();
                foreach ($arguments as $key => $argument) {
                    $orX->add($qb->expr()->like('cast(j.args as text)', ':args_' . $key));
                    $qb->setParameter('args_' . $key, '%' . $argument . '%');
                }

                $qb->andWhere($orX);
            } else {
                $qb->andWhere($qb->expr()->like('cast(j.args as text)', ':args'))
                    ->setParameter('args', '%' . $arguments . '%');
            }
        }

        return $qb;
    }

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @param string[]|string      $commandName
     * @param string[]|string|null $arguments
     * @param string[]|string|null $states
     *
     * @return QueryBuilder
     */
    protected function getQBSyncJobsCount($commandName, $arguments = null, $states = null)
    {
        $qb = $this->getQBSyncJobs($commandName, $arguments, $states);
        $qb->select('count(j.id)');

        return $qb;
    }

    /**
     * @deprecated TODO CRM-5838 remove it
     *
     * @param string               $commandName
     * @param string[]|string|null $arguments
     * @param string[]|string|null $states
     *
     * @return int
     */
    public function getSyncJobsCount($commandName, $states = null, $arguments = null)
    {
        /** @var QueryBuilder $qb */
        $qb = $this->getQBSyncJobsCount($commandName, $arguments, $states);

        return (int)$qb->getQuery()->getSingleScalarResult();
    }

    /**
     * Returns latest status for integration's connector and code if it exists.
     *
     * @param Integration $integration
     * @param string      $connector
     * @param int|null    $code
     *
     * @return Status|null
     */<commit_msg>CRM-6084: Remove depens to JMSJobQueue from ChannelRepository<commit_after><?php

namespace Oro\Bundle\IntegrationBundle\Entity\Repository;

use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Oro\Bundle\BatchBundle\ORM\Query\BufferedQueryResultIterator;
use Oro\Bundle\IntegrationBundle\Entity\Channel as Integration;
use Oro\Bundle\IntegrationBundle\Entity\Status;

class ChannelRepository extends EntityRepository
{
    const BUFFER_SIZE = 100;

    /**
     * Returns latest status for integration's connector and code if it exists.
     *
     * @param Integration $integration
     * @param string      $connector
     * @param int|null    $code
     *
     * @return Status|null
     */
{'fraction_non_alphanumeric': 0.14598432184588078, 'fraction_numerical': 0.0062120988019523735, 'mean_word_length': 2.2857142857142856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 64, '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': '5013906', 'n_tokens_mistral': 2057, 'n_tokens_neox': 1963, 'n_words': 372}