/**
  * @param DelegatingSourceCleaner $cleaner
  * @param OriginInterface         $origin
  * @param ThresholdVoterInterface $voter
  *
  * @return boolean
  */
 public function cleanOrigin(DelegatingSourceCleaner $cleaner, OriginInterface $origin, ThresholdVoterInterface $voter)
 {
     if (null === ($expireDate = $this->getLastFullImportDate($origin))) {
         $this->logger->debug(sprintf('Skipping origin %s, because it has no recent imports', $origin->getTitle()));
         $this->eventDispatcher->dispatch(IoEvents::ORIGIN_CLEANUP_SKIP, new OriginCleanupEvent($origin, 0));
         return false;
     }
     $this->eventDispatcher->dispatch(IoEvents::PRE_CLEAN_ORIGIN, new OriginEvent($origin));
     $this->logger->debug(sprintf('Checking sources of %s that have not been visited since %s', $origin->getTitle(), $expireDate->format('Y-m-d H:i:s')));
     // get sources that haven't been visited since $expireDate
     $sourceRepo = $this->sourceManager->getRepository();
     $count = $sourceRepo->countByOriginAndUnvisitedSince($origin, $expireDate);
     // fail safe: see if percentage of sources to be removed is not too high
     $total = $sourceRepo->countByOrigin($origin);
     $max = $this->getThreshold($total);
     // see if threshold is reached
     if ($count > $max) {
         $message = sprintf('Stopping cleanup for origin %s, because %s of %s sources were to be deleted, %s is the maximum.', $origin->getTitle(), $count, $total, $max);
         if (!$voter->vote($count, $total, $max, $message)) {
             $this->eventDispatcher->dispatch(IoEvents::ORIGIN_CLEANUP_HALT, new OriginCleanupHaltEvent($origin, $count, $total, $max));
             return false;
         }
     }
     $this->logger->debug(sprintf('Cleaning %d sources for origin %s', $count, $origin->getTitle()));
     $builder = $sourceRepo->queryByOriginAndUnvisitedSince($origin, $expireDate);
     $numCleaned = $cleaner->cleanByQuery($builder->getQuery());
     $this->eventDispatcher->dispatch(IoEvents::POST_CLEAN_ORIGIN, new OriginCleanupEvent($origin, $numCleaned));
     return $numCleaned;
 }
 /**
  * @param ThresholdVoterInterface $voter
  * @param array                   $origins
  *
  * @throws \RuntimeException
  *
  * @return integer
  */
 protected function clean(ThresholdVoterInterface $voter, array $origins = null)
 {
     if ($origins) {
         $idleCleaner = null;
         foreach ($this->sourceCleaner->getCleaners() as $cleaner) {
             if ($cleaner instanceof IdleSourceCleaner) {
                 $idleCleaner = $cleaner;
                 break;
             }
         }
         if (null === $idleCleaner) {
             throw new \RuntimeException('No IdleSourceCleaner is configured');
         }
         $repo = $this->originManager->getRepository();
         $query = $repo->createQueryBuilder('o')->select('o')->where('o.id IN (:ids)')->setParameter('ids', $origins)->getQuery();
         $numCleaned = 0;
         foreach ($query->getResult() as $origin) {
             $numCleaned += $idleCleaner->cleanOrigin($this->sourceCleaner, $origin, $voter);
         }
         return $numCleaned;
     } else {
         return $this->sourceCleaner->cleanAll($voter);
     }
 }
 /**
  * @inheritdoc
  */
 public function clean(DelegatingSourceCleaner $cleaner, ThresholdVoterInterface $voter)
 {
     $builder = $this->sourceManager->getRepository()->createQueryBuilder('s')->select('s')->innerJoin('s.origin', 'o')->where('SIZE(o.feeds) = 0');
     return $cleaner->cleanByQuery($builder->getQuery());
 }