Passed
Push — master ( 7e03c5...6dfa53 )
by Dev
34:30 queued 19:18
created

ShowExternalLinksCommand::execute()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 18
c 1
b 0
f 1
nc 4
nop 2
dl 0
loc 28
rs 8.4444
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler\Command;
4
5
use PiedWeb\SeoPocketCrawler\ExtractExternalLinks;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ShowExternalLinksCommand extends Command
14
{
15
    protected static $defaultName = 'crawler:external-links';
16
17
    protected $id;
18
19
    protected function configure()
20
    {
21
        $this->setDescription('List external domain linked.');
22
23
        $this
24
            ->addArgument(
25
                'id',
26
                InputArgument::REQUIRED,
27
                'id from a previous crawl'
28
            )
29
            ->addOption('host', 'ho', InputOption::VALUE_NONE, 'get only host')
30
        ;
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $table = new Table($output);
36
37
        $table->setHeaders($input->getOption('host') ? ['Host'] : ['url', 'from']);
38
39
        $links = ExtractExternalLinks::scan($input->getArgument('id'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('id') can also be of type null and string[]; however, parameter $id of PiedWeb\SeoPocketCrawler...ctExternalLinks::scan() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $links = ExtractExternalLinks::scan(/** @scrutinizer ignore-type */ $input->getArgument('id'));
Loading history...
40
        arsort($links);
41
        $ever = [];
42
        foreach ($links as $link => $from) {
43
            if ($input->getOption('host')) {
44
                $host = parse_url($link, PHP_URL_HOST);
45
                if ($host && !isset($ever[$host])) {
46
                    $ever[$host] = 1;
47
                    $table->addRow([$host]);
48
                }
49
            } else {
50
                $first = true;
51
                foreach ($from as $url) {
52
                    $table->addRow([true === $first ? $link : '', $url]);
53
                    $first = false;
54
                }
55
            }
56
        }
57
58
        $table->render();
59
60
        return 0;
61
    }
62
}
63