<?php

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Sandbox\SecurityError;
use Twig\Sandbox\SecurityNotAllowedTagError;
use Twig\Sandbox\SecurityNotAllowedFilterError;
use Twig\Sandbox\SecurityNotAllowedFunctionError;
use Twig\Source;
use Twig\Template;

/* @bundles/LeadBundle/Segment/Query/Filter/SegmentReferenceFilterQueryBuilder.php */
class __TwigTemplate_05b0ee7e47c2d5b396052e2939c9bb955412132fd0b8c92537d368a26cab46d9 extends Template
{
    private $source;
    private $macros = [];

    public function __construct(Environment $env)
    {
        parent::__construct($env);

        $this->source = $this->getSourceContext();

        $this->parent = false;

        $this->blocks = [
        ];
    }

    protected function doDisplay(array $context, array $blocks = [])
    {
        $macros = $this->macros;
        // line 1
        echo "<?php

namespace Mautic\\LeadBundle\\Segment\\Query\\Filter;

use Doctrine\\DBAL\\DBALException;
use Doctrine\\ORM\\EntityManager;
use Mautic\\LeadBundle\\Entity\\LeadList;
use Mautic\\LeadBundle\\Segment\\ContactSegmentFilter;
use Mautic\\LeadBundle\\Segment\\ContactSegmentFilterFactory;
use Mautic\\LeadBundle\\Segment\\Exception\\SegmentNotFoundException;
use Mautic\\LeadBundle\\Segment\\Exception\\SegmentQueryException;
use Mautic\\LeadBundle\\Segment\\Query\\ContactSegmentQueryBuilder;
use Mautic\\LeadBundle\\Segment\\Query\\Expression\\CompositeExpression;
use Mautic\\LeadBundle\\Segment\\Query\\QueryBuilder;
use Mautic\\LeadBundle\\Segment\\Query\\QueryException;
use Mautic\\LeadBundle\\Segment\\RandomParameterName;
use Symfony\\Component\\EventDispatcher\\EventDispatcherInterface;

class SegmentReferenceFilterQueryBuilder extends BaseFilterQueryBuilder
{
    /**
     * @var ContactSegmentQueryBuilder
     */
    private \$leadSegmentQueryBuilder;

    /**
     * @var ContactSegmentFilterFactory
     */
    private \$leadSegmentFilterFactory;

    /**
     * @var EntityManager
     */
    private \$entityManager;

    public function __construct(
        RandomParameterName \$randomParameterNameService,
        ContactSegmentQueryBuilder \$leadSegmentQueryBuilder,
        EntityManager \$entityManager,
        ContactSegmentFilterFactory \$leadSegmentFilterFactory,
        EventDispatcherInterface \$dispatcher
    ) {
        parent::__construct(\$randomParameterNameService, \$dispatcher);

        \$this->leadSegmentQueryBuilder  = \$leadSegmentQueryBuilder;
        \$this->leadSegmentFilterFactory = \$leadSegmentFilterFactory;
        \$this->entityManager            = \$entityManager;
    }

    /**
     * @return string
     */
    public static function getServiceId()
    {
        return 'mautic.lead.query.builder.special.leadlist';
    }

    /**
     * @throws SegmentNotFoundException
     * @throws SegmentQueryException
     * @throws DBALException
     * @throws QueryException
     */
    public function applyQuery(QueryBuilder \$queryBuilder, ContactSegmentFilter \$filter)
    {
        \$leadsTableAlias = \$queryBuilder->getTableAlias(MAUTIC_TABLE_PREFIX.'leads');
        \$segmentIds      = \$filter->getParameterValue();

        if (!is_array(\$segmentIds)) {
            \$segmentIds = [intval(\$segmentIds)];
        }

        \$orLogic = [];

        foreach (\$segmentIds as \$segmentId) {
            \$exclusion = in_array(\$filter->getOperator(), ['notExists', 'notIn']);

            /** @var LeadList \$contactSegment */
            \$contactSegment = \$this->entityManager->getRepository('MauticLeadBundle:LeadList')->find(\$segmentId);
            if (!\$contactSegment) {
                throw new SegmentNotFoundException(sprintf('Segment %d used in the filter does not exist anymore.', \$segmentId));
            }

            \$filters = \$this->leadSegmentFilterFactory->getSegmentFilters(\$contactSegment);

            \$segmentQueryBuilder       = \$this->leadSegmentQueryBuilder->assembleContactsSegmentQueryBuilder(\$contactSegment->getId(), \$filters, true);
            \$subSegmentLeadsTableAlias = \$segmentQueryBuilder->getTableAlias(MAUTIC_TABLE_PREFIX.'leads');
            \$segmentQueryBuilder->resetQueryParts(['select'])->select('null');

            //  If the segment contains no filters; it means its for manually subscribed only
            if (count(\$filters)) {
                \$segmentQueryBuilder = \$this->leadSegmentQueryBuilder->addManuallyUnsubscribedQuery(\$segmentQueryBuilder, (int) \$contactSegment->getId());
            }

            \$segmentQueryBuilder = \$this->leadSegmentQueryBuilder->addManuallySubscribedQuery(\$segmentQueryBuilder, (int) \$contactSegment->getId());

            \$parameters = \$segmentQueryBuilder->getParameters();
            foreach (\$parameters as \$key => \$value) {
                \$queryBuilder->setParameter(\$key, \$value);
            }

            \$this->leadSegmentQueryBuilder->queryBuilderGenerated(\$contactSegment, \$segmentQueryBuilder);

            \$segmentQueryWherePart = \$segmentQueryBuilder->getQueryPart('where');
            \$segmentQueryBuilder->where(\"\$leadsTableAlias.id = \$subSegmentLeadsTableAlias.id\");
            \$segmentQueryBuilder->andWhere(\$segmentQueryWherePart);

            if (\$exclusion) {
                \$expression = \$queryBuilder->expr()->notExists(\$segmentQueryBuilder->getSQL());
            } else {
                \$expression = \$queryBuilder->expr()->exists(\$segmentQueryBuilder->getSQL());
            }

            if (!\$exclusion && count(\$segmentIds) > 1) {
                \$orLogic[] = \$expression;
            } else {
                \$queryBuilder->addLogic(\$expression, \$filter->getGlue());
            }
        }

        if (count(\$orLogic)) {
            \$queryBuilder->addLogic(new CompositeExpression(CompositeExpression::TYPE_OR, \$orLogic), CompositeExpression::TYPE_AND);
        }

        return \$queryBuilder;
    }
}
";
    }

    public function getTemplateName()
    {
        return "@bundles/LeadBundle/Segment/Query/Filter/SegmentReferenceFilterQueryBuilder.php";
    }

    public function getDebugInfo()
    {
        return array (  37 => 1,);
    }

    public function getSourceContext()
    {
        return new Source("", "@bundles/LeadBundle/Segment/Query/Filter/SegmentReferenceFilterQueryBuilder.php", "/var/www/mautic/app/bundles/LeadBundle/Segment/Query/Filter/SegmentReferenceFilterQueryBuilder.php");
    }
}
