/**
  * @param StaticSegment $staticSegment
  *
  * {@inheritdoc}
  */
 protected function createSubordinateIterator($staticSegment)
 {
     $this->assertRequiredDependencies();
     if (!$staticSegment->getExtendedMergeVars()) {
         return new \EmptyIterator();
     }
     $qb = $this->getIteratorQueryBuilder($staticSegment);
     $marketingList = $staticSegment->getMarketingList();
     $memberIdentifier = MarketingListQueryBuilderAdapter::MEMBER_ALIAS . '.id';
     $fieldExpr = $this->fieldHelper->getFieldExpr($marketingList->getEntity(), $qb, $this->doctrineHelper->getSingleEntityIdentifierFieldName($marketingList->getEntity()));
     $qb->addSelect($fieldExpr . ' AS entity_id');
     $qb->addSelect($memberIdentifier . ' AS member_id');
     $qb->andWhere($qb->expr()->andX($qb->expr()->isNotNull($memberIdentifier)));
     $bufferedIterator = new BufferedQueryResultIterator($qb);
     $bufferedIterator->setHydrationMode(AbstractQuery::HYDRATE_ARRAY)->setReverse(true);
     $uniqueMembers =& $this->uniqueMembers;
     return new \CallbackFilterIterator($bufferedIterator, function (&$current) use($staticSegment, &$uniqueMembers) {
         if (is_array($current)) {
             if (!empty($current['member_id']) && in_array($current['member_id'], $uniqueMembers, true)) {
                 return false;
             }
             $current['subscribersList_id'] = $staticSegment->getSubscribersList()->getId();
             $current['static_segment_id'] = $staticSegment->getId();
             $uniqueMembers[] = $current['member_id'];
             unset($current['id']);
         }
         return true;
     });
 }
 /**
  * @param string $entityClass
  * @param string $fieldName
  * @param string $alias
  * @param array $fieldConfig
  * @param array $joins
  * @param string $expected
  *
  * @dataProvider virtualFieldsProvider
  */
 public function testGetFieldExprVirtual($entityClass, $fieldName, $alias, array $fieldConfig, array $joins, $expected)
 {
     $from = $this->getMockBuilder('Doctrine\\ORM\\Query\\Expr\\From')->disableOriginalConstructor()->getMock();
     $from->expects($this->atLeastOnce())->method('getAlias')->will($this->returnValue($alias));
     /** @var \PHPUnit_Framework_MockObject_MockObject|QueryBuilder $qb */
     $qb = $this->getMockBuilder('Doctrine\\ORM\\QueryBuilder')->disableOriginalConstructor()->getMock();
     $qb->expects($this->atLeastOnce())->method('getDQLPart')->will($this->returnValueMap([['from', [$from]], ['join', $joins]]));
     $this->virtualFieldProvider->expects($this->once())->method('isVirtualField')->with($entityClass, $fieldName)->will($this->returnValue(true));
     $this->virtualFieldProvider->expects($this->once())->method('getVirtualFieldQuery')->with($entityClass, $fieldName)->will($this->returnValue($fieldConfig));
     $this->assertEquals($expected, $this->helper->getFieldExpr($entityClass, $qb, $fieldName));
 }
 /**
  * @param MarketingList $marketingList
  * @param string $email
  * @return QueryBuilder
  */
 protected function getMarketingListEntitiesByEmailQueryBuilder(MarketingList $marketingList, $email)
 {
     $emailFields = $this->contactInformationFieldsProvider->getMarketingListTypedFields($marketingList, ContactInformationFieldsProvider::CONTACT_INFORMATION_SCOPE_EMAIL);
     $qb = $this->getEntitiesQueryBuilder($marketingList);
     $expr = $qb->expr()->orX();
     foreach ($emailFields as $emailField) {
         $parameterName = $emailField . mt_rand();
         $expr->add($qb->expr()->eq($this->fieldHelper->getFieldExpr($marketingList->getEntity(), $qb, $emailField), ':' . $parameterName));
         $qb->setParameter($parameterName, $email);
     }
     $qb->andWhere($expr);
     return $qb;
 }
 /**
  * @param StaticSegment $staticSegment
  * @param QueryBuilder $qb
  * @return void
  */
 public function prepareMarketingListEntities(StaticSegment $staticSegment, QueryBuilder $qb)
 {
     if (!$this->memberClassName) {
         throw new \InvalidArgumentException('Member class name must be provided');
     }
     $marketingList = $staticSegment->getMarketingList();
     $contactInformationFields = $this->contactInformationFieldsProvider->getMarketingListTypedFields($marketingList, ContactInformationFieldsProvider::CONTACT_INFORMATION_SCOPE_EMAIL);
     $expr = $qb->expr()->orX();
     foreach ($contactInformationFields as $contactInformationField) {
         $contactInformationFieldExpr = $this->fieldHelper->getFieldExpr($marketingList->getEntity(), $qb, $contactInformationField);
         $qb->addSelect($contactInformationFieldExpr . ' AS ' . $contactInformationField);
         $expr->add($qb->expr()->eq($contactInformationFieldExpr, sprintf('%s.%s', self::MEMBER_ALIAS, self::MEMBER_EMAIL_FIELD)));
     }
     $organization = $staticSegment->getChannel()->getOrganization();
     $metadata = $this->ownershipMetadataProvider->getMetadata($marketingList->getEntity());
     if ($organization && ($fieldName = $metadata->getOrganizationFieldName())) {
         $aliases = $qb->getRootAliases();
         $qb->andWhere($qb->expr()->eq(sprintf('%s.%s', reset($aliases), $fieldName), ':organization'));
         $qb->setParameter('organization', $organization);
     }
     $qb->leftJoin($this->memberClassName, self::MEMBER_ALIAS, Join::WITH, $qb->expr()->andX($expr, $qb->expr()->eq(sprintf('%s.subscribersList', self::MEMBER_ALIAS), ':subscribersList')))->setParameter('subscribersList', $staticSegment->getSubscribersList()->getId());
 }