コード例 #1
0
 /**
  * Creates an import for a feed. If an import for this feed was created
  * before, but has not started yet, that import is returned. All other open
  * imports are closed first.
  *
  * @param FeedEntity $feed
  * @param \DateTime  $scheduleDate
  * @param boolean    $forced
  * @param boolean    $partial      If left out, it will be determined based on feed
  *
  * @throws \RuntimeException
  * @return Import
  */
 public function createImport(FeedEntity $feed, \DateTime $scheduleDate = null, $forced = false, $partial = null)
 {
     $entityManager = $this->getEntityManager();
     $repo = $entityManager->getRepository('FMIoBundle:Import');
     /** @var $imports Import[] */
     $imports = $repo->findByFeed($feed);
     if (is_null($scheduleDate)) {
         $scheduleDate = new \DateTime();
     }
     if (is_null($partial)) {
         $partial = $feed->isPartial();
     }
     // see if any imports are still unfinished
     foreach ($imports as $import) {
         // skip finished imports
         if ($this->isImportFinished($import)) {
             continue;
         }
         // if it hasn't started yet, use this one
         if (!$this->isImportStarted($import)) {
             return $import;
         }
         // unfinished import, see if it has any remaining parts
         if ($this->importHasUnfinishedParts($import)) {
             throw new \RuntimeException(sprintf('Import %d has unfinished parts, close those first before creating a new import', $import->getId()));
         }
         // we can safely close it
         $this->finishImport($import);
     }
     // all previous imports are checked and finished if necessary, we can
     // create a new one now
     $import = new Import();
     $import->setFeed($feed);
     $import->setForced($forced);
     $import->setPartial($partial);
     $import->setDatetimeScheduled($scheduleDate);
     // persist now: we want the import on record before starting it
     $entityManager->persist($import);
     $entityManager->flush($import);
     return $import;
 }