ShowExternalLinksCommand   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 29
c 1
b 0
f 1
dl 0
loc 49
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
B execute() 0 28 8
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
                .PHP_EOL.'You can use `last` to get show external links from the last crawl.'
29
            )
30
            ->addOption('host', 'ho', InputOption::VALUE_NONE, 'get only host')
31
        ;
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $table = new Table($output);
37
38
        $table->setHeaders($input->getOption('host') ? ['Host'] : ['url', 'from']);
39
40
        $links = ExtractExternalLinks::scan((string) $input->getArgument('id'));
41
        arsort($links);
42
        $ever = [];
43
        foreach ($links as $link => $from) {
44
            if ($input->getOption('host')) {
45
                $host = parse_url($link, PHP_URL_HOST);
46
                if ($host && ! isset($ever[$host])) {
47
                    $ever[$host] = 1;
48
                    $table->addRow([$host]);
49
                }
50
            } else {
51
                $first = true;
52
                foreach ($from as $url) {
53
                    $table->addRow([true === $first ? $link : '', $url]);
54
                    $first = false;
55
                }
56
            }
57
        }
58
59
        $table->render();
60
61
        return 0;
62
    }
63
}
64