ParseCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 72
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A initialize() 0 7 1
A execute() 0 12 2
A parse() 0 10 2
1
<?php
2
3
namespace EmanueleMinotto\DomainParserBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Parses an URL or a host.
13
 *
14
 * @author Emanuele Minotto <[email protected]>
15
 */
16
class ParseCommand extends ContainerAwareCommand
17
{
18
    /**
19
     * Pdp Parser.
20
     *
21
     * @var \Pdp\Parser
22
     */
23
    private $parser;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 6
    protected function configure()
29
    {
30 6
        $this
31 6
            ->setName('domain-parser:parse')
32 6
            ->setDescription('Parses URL')
33 6
            ->addArgument('url', InputArgument::REQUIRED, 'URL to parse')
34 6
            ->addOption(
35 6
                'host-only',
36 6
                null,
37 6
                InputOption::VALUE_NONE,
38
                'Parses host only'
39 6
            );
40 6
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 6
    protected function initialize(
46
        InputInterface $input,
47
        OutputInterface $output
48
    ) {
49 6
        $container = $this->getContainer();
50 6
        $this->parser = $container->get('pdp.parser');
51 6
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 6
    protected function execute(InputInterface $input, OutputInterface $output)
57
    {
58 6
        $parsed = $this->parse(
59 6
            $input->getArgument('url'),
60 6
            $input->getOption('host-only')
61 6
        );
62 6
        $output->writeln(sprintf('Parsed: <info>%s</info>', $parsed));
63
64 6
        foreach ($parsed->toArray() as $key => $val) {
65 6
            $output->writeln(sprintf(' <comment>%s</comment>: %s', $key, $val));
66 6
        }
67 6
    }
68
69
    /**
70
     * Argument parsing.
71
     *
72
     * @param string $argument Url or domain
73
     * @param bool   $hostOnly
74
     *
75
     * @return \Pdp\Uri\Url|\Pdp\Uri\Url\Host
76
     */
77 6
    private function parse($argument, $hostOnly = false)
78
    {
79 6
        $parsed = $this->parser->parseUrl($argument);
80
81 6
        if ($hostOnly) {
82 3
            return $parsed->host;
83
        }
84
85 3
        return $parsed;
86
    }
87
}
88