/**
  * @param StaticSegment $staticSegment
  * @param string $segmentStateFilter
  * @param string $method
  * @param string $itemState
  * @param bool $deleteMember
  * @return array
  */
 public function handleMembersUpdate(StaticSegment $staticSegment, $segmentStateFilter, $method, $itemState, $deleteMember = false)
 {
     $itemsToWrite = [];
     $items = $staticSegment->getSegmentMembers()->filter(function (StaticSegmentMember $segmentMember) use($segmentStateFilter) {
         return $segmentMember->getState() === $segmentStateFilter;
     })->toArray();
     if (empty($items)) {
         return [];
     }
     $emails = array_map(function (StaticSegmentMember $segmentMember) {
         return $segmentMember->getMember()->getEmail();
     }, $items);
     $response = $this->transport->{$method}(['id' => $staticSegment->getSubscribersList()->getOriginId(), 'seg_id' => (int) $staticSegment->getOriginId(), 'batch' => array_map(function ($email) {
         return ['email' => $email];
     }, $emails), 'delete_member' => $deleteMember]);
     $this->handleResponse($response, function ($response, LoggerInterface $logger) use($staticSegment) {
         $logger->info(sprintf('Segment #%s [origin_id=%s] Members: [%s] add, [%s] error', $staticSegment->getId(), $staticSegment->getOriginId(), $response['success_count'], $response['error_count']));
     });
     $emailsWithErrors = $this->getArrayData($response, 'errors');
     /** @var StaticSegmentMember[]|ArrayCollection $items */
     $items = new ArrayCollection($items);
     $items->filter(function (StaticSegmentMember $segmentMember) use($emailsWithErrors) {
         return !in_array($segmentMember->getMember()->getEmail(), $emailsWithErrors, true);
     });
     foreach ($items as $item) {
         $item->setState($itemState);
         $this->logger->debug(sprintf('Member with id "%s" and email "%s" got "%s" state', $item->getMember()->getOriginId(), $item->getMember()->getEmail(), $itemState));
         $itemsToWrite[] = $item;
     }
     return $itemsToWrite;
 }
 /**
  * @param StaticSegment $entity
  */
 protected function onSuccess($entity)
 {
     // Reset originId of static segment if subscribers list was changed to force list creation
     if ($this->oldSubscribersList && $this->oldSubscribersList->getId() != $entity->getSubscribersList()->getId()) {
         $entity->setOriginId(null);
     }
     parent::onSuccess($entity);
 }
 /**
  * @param StaticSegment $value
  * @param UniqueStaticSegmentNameConstraint|Constraint $constraint
  */
 public function validate($value, Constraint $constraint)
 {
     if ($value instanceof StaticSegment && !$value->getOriginId()) {
         $this->transport->init($value->getChannel()->getTransport());
         $segments = $this->transport->getListStaticSegments($value->getSubscribersList());
         foreach ($segments as $segment) {
             if ($segment['name'] == $value->getName()) {
                 $this->context->addViolationAt('name', $constraint->message);
                 break;
             }
         }
     }
 }
 /**
  * @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());
 }