/**
  * @param AbstractQuery $query
  *
  * @return integer
  *
  * @throws \LogicException
  */
 public function cleanByQuery(AbstractQuery $query)
 {
     $numCleaned = 0;
     /** @var SourceInterface $source */
     foreach ($query->iterate() as list($source)) {
         if (!$source instanceof SourceInterface) {
             throw new \LogicException(sprintf('Invalid iterator given, encountered %s instead of SourceInterface', is_object($source) ? get_class($source) : gettype($source)));
         }
         $this->eventDispatcher->dispatch(IoEvents::PRE_CLEAN_SOURCE, new SourceEvent($source));
         $this->sourceManager->remove($source);
         $this->eventDispatcher->dispatch(IoEvents::POST_CLEAN_SOURCE, new SourceEvent($source));
         $numCleaned++;
         if ($numCleaned % 50 === 0) {
             $this->sourceManager->flush();
             $this->sourceManager->clear();
         }
     }
     if ($numCleaned > 0) {
         $this->sourceManager->flush();
         $this->sourceManager->clear();
     }
     return $numCleaned;
 }
 /**
  * @inheritdoc
  */
 public function remove(SourceInterface $source)
 {
     $this->sourceManager->remove($source);
 }