ScrapeRevisitUrlsCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 18
cp 0
rs 9.568
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 20
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use Doctrine\Common\Persistence\ManagerRegistry;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use TreeHouse\IoBundle\Entity\Scraper;
11
use TreeHouse\IoBundle\Entity\Scraper as ScraperEntity;
12
use TreeHouse\IoBundle\Model\SourceInterface;
13
use TreeHouse\IoBundle\Scrape\Exception\CrawlException;
14
use TreeHouse\IoBundle\Scrape\SourceRevisitor;
15
use TreeHouse\IoBundle\Source\SourceManagerInterface;
16
17
class ScrapeRevisitUrlsCommand extends Command
18
{
19
    /**
20
     * @var ManagerRegistry
21
     */
22
    protected $doctrine;
23
24
    /**
25
     * @var SourceManagerInterface
26
     */
27
    protected $sourceManager;
28
29
    /**
30
     * @var SourceRevisitor
31
     */
32
    protected $revisitor;
33
34
    /**
35
     * @param ManagerRegistry        $doctrine
36
     * @param SourceManagerInterface $sourceManager
37
     * @param SourceRevisitor        $revisitor
38
     */
39 View Code Duplication
    public function __construct(ManagerRegistry $doctrine, SourceManagerInterface $sourceManager, SourceRevisitor $revisitor)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->doctrine = $doctrine;
42
        $this->sourceManager = $sourceManager;
43
        $this->revisitor = $revisitor;
44
45
        parent::__construct();
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $this->setName('io:scrape:revisit-urls');
54
        $this->setDescription('Revisits earlier scraped sources to see if they still exist');
55
        $this->addOption('scraper', InputOption::VALUE_OPTIONAL, 'Limit the sources to a specific scraper id');
56
        $this->addOption(
57
            'async',
58
            'a',
59
            InputOption::VALUE_NONE,
60
            'Whether to revisit asynchronous. Doing so will queue sources, rather them revisiting them right away'
61
        );
62
        $this->addOption('no-limit', null, InputOption::VALUE_NONE, 'Disables the rate limit');
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    protected function execute(InputInterface $input, OutputInterface $output)
69
    {
70
        $async = $input->getOption('async');
71
        $noLimit = $input->getOption('no-limit');
72
        $scrapers = $this->findScrapers($input->getArgument('scraper'));
0 ignored issues
show
Documentation introduced by
$input->getArgument('scraper') is of type string|array<integer,string>|null, but the function expects a array<integer,integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
        foreach ($scrapers as $scraperEntity) {
75
            $date = new \DateTime(sprintf('-%d hours', $scraperEntity->getRevisitFrequency()));
76
77
            $builder = $this->sourceManager->getRepository()->queryByScraperAndUnvisitedSince($scraperEntity, $date);
78
            foreach ($builder->getQuery()->iterate() as list($source)) {
79
                /* @var SourceInterface $source */
80
                try {
81
                    $output->writeln(sprintf('Revisiting <info>%s</info>', $source->getOriginalUrl()));
82
83
                    $this->revisitor->revisit($source, $async, $noLimit);
84
                } catch (CrawlException $e) {
85
                    $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param integer[] $ids
93
     *
94
     * @return ScraperEntity[]
95
     */
96 View Code Duplication
    protected function findScrapers(array $ids)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $repo = $this->doctrine->getRepository('TreeHouseIoBundle:Scraper');
99
100
        if (!empty($ids)) {
101
            return $repo->findBy(['id' => $ids]);
102
        }
103
104
        return $repo->findAll();
105
    }
106
}
107