Demo entry 6662952

test

   

Submitted by anonymous on Nov 28, 2017 at 18:32
Language: PHP. Code size: 4.4 kB.

<?php

namespace AppBundle\Command;

use AppBundle\Entity\NewsProviderResource;
use AppBundle\EventSubscriber\KernelException\EntityManagerClosedExceptionSubscriber;
use AppBundle\Exception\CriticalException;
use AppBundle\Exception\WarningException;
use AppBundle\Service\NewsReader;
use AppBundle\Service\NewsValidator;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * @see SymfonyStyle
 */
class NewsReaderCommand extends Command
{
    use LockableTrait;

    /** @var NewsReader */
    private $newsReader;

    /** @var EntityManagerClosedExceptionSubscriber */
    private $exceptionSubscriber;

    /** @var RegistryInterface */
    private $doctrine;

    public function __construct(NewsReader $newsReader, RegistryInterface $doctrine, EntityManagerClosedExceptionSubscriber $exceptionSubscriber)
    {
        $this->newsReader = $newsReader;
        $this->exceptionSubscriber = $exceptionSubscriber;
        $this->doctrine = $doctrine;
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName('app:news_reader:read')
            ->setDescription('Import all news');
    }

    protected function execute(InputInterface $input, OutputInterface $output): ?int
    {
        if (!$this->lock()) {
            $output->writeln('<error>The command is already running in another process.</error>');

            return 0;
        }

        $allResources = $this->doctrine->getRepository(NewsProviderResource::class)->findAll();

        foreach ($allResources as $resource) {
            $this->populateFromResource($resource, $output);
        }
        $this->release();

        return 0;
    }

    private function populateFromResource(NewsProviderResource $resource, OutputInterface $output)
    {
        try {
            $output->writeln(sprintf('Reading %s', $resource->getName()));
            $urlsToParse = $this->newsReader->getUrlsToParse($resource);
            $count = 0;
            foreach ($urlsToParse as $url) {
                $success = $this->createPost($url, $resource, $output);
                if ($success) {
                    $count++;
                }
            }
            $message = sprintf('Fetched %d new articles from %s', $count, (string)$resource);
            $output->writeln(str_repeat('-', \strlen($message)));
            $output->writeln($message);
            $output->writeln(str_repeat('-', \strlen($message)));
        } catch (\Exception $e) {
            $output->writeln(sprintf('Exception caught "%s", message "%s"', \get_class($e), $e->getMessage()));
        }
    }

    private function createPost(string $url, NewsProviderResource $resource, OutputInterface $output): bool
    {
        try {
            $post = $this->newsReader->createPostFromUrl($url, $resource);
            $output->writeln(sprintf('<info>Post "%s" from "%s" assigned to category "%s"</info>', $post->getTitle(), $resource->getName(), (string)$resource->getCategory()));

            return true;
        }
        catch (CriticalException $e) {
            $output->writeln(sprintf('<error>Critical exception with message: %s</error>', mb_strimwidth($e->getMessage(), 0, 1000)));
        }
        catch (WarningException $e) {
            $output->writeln(sprintf('<comment>%s</comment>', $e->getMessage()));
        }
        catch (\Doctrine\DBAL\Exception\DriverException |\Doctrine\ORM\ORMException |\PDOException $e) {
            $this->exceptionSubscriber->resetAll();
            $output->writeln('Resetting Entity manager and repositories');
            $output->writeln(sprintf('<error>Critical exception with message: %s</error>', mb_strimwidth($e->getMessage(), 0, 1000)));
        }
        catch (\Exception $e) { // fallback for all other exceptions
//            $this->exceptionSubscriber->resetAll($output);
            $output->writeln(sprintf('<error>Exception thrown "%s" thrown with message "%s"</error>', \get_class($e), $e->getMessage()));
        }

        return false;
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).