/**
  * @param StaticSegment $staticSegment
  *
  * @throws \InvalidArgumentException
  * @return QueryBuilder
  */
 protected function getIteratorQueryBuilder(StaticSegment $staticSegment)
 {
     $marketingList = $staticSegment->getMarketingList();
     $qb = $this->marketingListProvider->getMarketingListEntitiesQueryBuilder($marketingList, MarketingListProvider::FULL_ENTITIES_MIXIN);
     $this->prepareIteratorPart($qb);
     /** @var From[] $from */
     $from = $qb->getDQLPart('from');
     $entityAlias = $from[0]->getAlias();
     $parts = $this->formatter->extractNamePartsPaths($marketingList->getEntity(), $entityAlias);
     $qb->resetDQLPart('select');
     if (isset($parts['first_name'])) {
         $qb->addSelect(sprintf('%s AS %s', $parts['first_name'], MemberSyncDataConverter::FIRST_NAME_KEY));
     }
     if (isset($parts['last_name'])) {
         $qb->addSelect(sprintf('%s AS %s', $parts['last_name'], MemberSyncDataConverter::LAST_NAME_KEY));
     }
     $this->marketingListQueryBuilderAdapter->prepareMarketingListEntities($staticSegment, $qb);
     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());
 }
 /**
  * {@inheritdoc}
  */
 protected function getIteratorQueryBuilder(StaticSegment $staticSegment)
 {
     $marketingList = $staticSegment->getMarketingList();
     if ($marketingList->isManual()) {
         $mixin = MarketingListProvider::MANUAL_RESULT_ENTITIES_MIXIN;
     } else {
         $mixin = MarketingListProvider::RESULT_ENTITIES_MIXIN;
     }
     $qb = clone $this->marketingListProvider->getMarketingListQueryBuilder($marketingList, $mixin);
     $this->marketingListQueryBuilderAdapter->prepareMarketingListEntities($staticSegment, $qb);
     return $qb;
 }