public function load(ObjectManager $manager)
 {
     $segments = $manager->getRepository('OroSegmentBundle:Segment')->findAll();
     $entities = $manager->getRepository('OroTestFrameworkBundle:WorkflowAwareEntity')->findAll();
     $entityCount = count($entities);
     /** @var Segment $segment */
     foreach ($segments as $segment) {
         $randomStart = rand(0, $entityCount);
         $randomEnd = rand($randomStart, $entityCount - $randomStart);
         /** @var WorkflowAwareEntity $entity */
         foreach ($entities as $key => $entity) {
             if ($key < $randomStart) {
                 continue;
             }
             if ($key > $randomEnd) {
                 break;
             }
             $segmentSnapshot = new SegmentSnapshot($segment);
             $segmentSnapshot->setEntityId($entity->getId());
             $segmentSnapshot->setCreatedAt(new \DateTime());
             $manager->persist($segmentSnapshot);
         }
     }
     $manager->flush();
 }
 /**
  * Runs static repository restriction query and stores it state into snapshot entity
  *
  * @param Segment $segment
  *
  * @throws \LogicException
  * @throws \Exception
  */
 public function run(Segment $segment)
 {
     if ($segment->getType()->getName() !== SegmentType::TYPE_STATIC) {
         throw new \LogicException('Only static segments could have snapshots.');
     }
     $this->em->getRepository('OroSegmentBundle:SegmentSnapshot')->removeBySegment($segment);
     $qb = $this->dynamicSegmentQB->build($segment);
     $iterator = new BufferedQueryResultIterator($qb);
     $writeCount = 0;
     try {
         $this->em->beginTransaction();
         $this->em->clear(ClassUtils::getClass($segment));
         foreach ($iterator as $data) {
             // only not composite identifiers are supported
             $id = reset($data);
             $writeCount++;
             /** @var Segment $reference */
             $reference = $this->em->getReference(ClassUtils::getClass($segment), $segment->getId());
             $snapshot = new SegmentSnapshot($reference);
             $snapshot->setEntityId($id);
             $this->toWrite[] = $snapshot;
             if (0 === $writeCount % $this->batchSize) {
                 $this->write($this->toWrite);
                 $this->toWrite = [];
             }
         }
         if (count($this->toWrite) > 0) {
             $this->write($this->toWrite);
         }
         $this->em->commit();
     } catch (\Exception $exception) {
         $this->em->rollback();
         throw $exception;
     }
     $segment = $this->em->merge($segment);
     $segment->setLastRun(new \DateTime('now', new \DateTimeZone('UTC')));
     $this->em->persist($segment);
     $this->em->flush();
 }
 public function testSettersAndGetters()
 {
     $this->assertNull($this->entity->getId());
     $this->assertNull($this->entity->getCreatedAt());
     $this->assertNull($this->entity->getEntityId());
     $this->assertNotNull($this->entity->getSegment());
     $testEntityId = 12;
     $testCreatedAt = new \DateTime('now - 1 day', new \DateTimeZone('UTC'));
     $this->entity->setEntityId($testEntityId);
     $this->entity->setCreatedAt($testCreatedAt);
     $this->assertSame($testEntityId, $this->entity->getEntityId());
     $this->assertSame($testCreatedAt, $this->entity->getCreatedAt());
     $this->entity->prePersist();
     $this->assertNotSame($testCreatedAt, $this->entity->getCreatedAt());
     $this->assertInstanceOf('\\DateTime', $this->entity->getCreatedAt());
 }