public function save(array $publications) { if (0 === count($publications)) { return; } $repository = $this->_em->getRepository('ECENetagoraBundle:KnownLink'); $references = $this->getExistingReferences($publications); foreach ($publications as $publication) { if (!in_array($publication->getReference(), $references)) { // Scrap the publication and get the latest uri try { $response = $this->browser->get($publication->getLinkUrl()); } catch (\Exception $e) { // The Http request failed... continue; } $urls = explode("\n", $response->getHeader('Location')); $url = array_pop($urls); $knownLink = $this->_em->getRepository('ECENetagoraBundle:KnownLink')->findOneByUrl($url); // We found a corresponding KnownLink that we can affect to the publication if ($knownLink) { $publication->setKnownLink($knownLink); $this->_em->persist($publication); continue; } // Otherwise, we need to create a new KnownLink and guess the category $crawler = new Crawler(); $crawler->addContent($response->getContent()); $guesser = new CategoryGuesser($url, $response, $crawler); $guesser->guess(); $category = $this->_em->getRepository('ECENetagoraBundle:Category')->findOneByType($guesser->getCategory()); /*if (null === $category) { var_dump($guesser->getScores());die; echo $guesser->getCategory();die; }*/ $knownLink = new KnownLink(); $knownLink->setCategory($category); $knownLink->setUrl($url); $knownLink->fromArray($guesser->getMetadata()); $publication->setKnownLink($knownLink); $this->_em->persist($knownLink); $this->_em->persist($publication); } } $this->_em->flush(); }
public function getCategory() { if ($this->knownLink) { return $this->knownLink->getCategory(); } }