Created
January 13, 2016 15:23
-
-
Save igorRovenki/b241fecd107281997bde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Command; | |
use AppBundle\Entity\Event; | |
use Debril\RssAtomBundle\Protocol\Parser\Item; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
class AddNewEventsCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('app:add-new-events') | |
->setDescription('Checks rss feeds and adds new events') | |
; | |
} | |
/** | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* @return int|null|void | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$em = $this->getContainer()->get('doctrine')->getManager(); | |
$webSites = $em->getRepository('AppBundle:WebSite')->findAll(); | |
$reader = $this->getContainer()->get('debril.reader'); | |
foreach ($webSites as $site) { | |
try { | |
$output->writeln(sprintf('Getting new events from <info>"%s"</info>', $site->getDomainCanonical())); | |
$feed = $reader->getFeedContent($site->getRssFeed()); | |
/** @var Item $item */ | |
foreach ($feed->getItems() as $item) { | |
$foundEvent = $em->getRepository('AppBundle:Event')->findOneByLink($item->getLink()); | |
if ($foundEvent instanceof Event) { | |
continue; | |
} | |
$event = new Event(); | |
$event->setTitle($item->getTitle()); | |
$event->setLink($item->getLink()); | |
$event->setPublishedAt($item->getUpdated()); | |
$event->setWebSite($site); | |
$em->persist($event); | |
$em->flush(); | |
$output->writeln(sprintf('<info>"%s"</info> successfully added', $event->getTitle())); | |
} | |
} catch (\Exception $e) { | |
$output->writeln(sprintf('Error <error>"%s"</error>', $e->getMessage())); | |
$output->writeln(sprintf('Trace <error>"%s"</error>', $e->getTraceAsString())); | |
$this->getContainer()->get('logger')->critical(sprintf('Error <error>"%s"</error>', $e->getMessage())); | |
$this->getContainer() | |
->get('logger') | |
->critical(sprintf('Trace <error>"%s"</error>', $e->getTraceAsString())) | |
; | |
} | |
} | |
$output->writeln('<info>Finished!</info>'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment