HomeCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
cbo 7
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
A execute() 0 32 4
A getDefaultBrowser() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Massimiliano Arione <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Command;
13
14
use Bowerphp\Package\Package;
15
use Bowerphp\Util\PackageNameVersionExtractor;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Process\Process;
20
21
/**
22
 * Home
23
 */
24
class HomeCommand extends Command
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('home')
33
            ->setDescription('Opens a package homepage into your favorite browser')
34
            ->addArgument('package', InputArgument::REQUIRED, 'Choose a package.')
35
            ->setHelp(<<<'EOT'
36
The <info>%command.name%</info> command opens a package homepage into your favorite browser.
37
38
  <info>php %command.full_name% name</info>
39
EOT
40
            )
41
        ;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->setGithubToken($output);
50
51
        $packageName = $input->getArgument('package');
52
53
        $packageNameVersion = PackageNameVersionExtractor::fromString($packageName);
54
55
        $package = new Package($packageNameVersion->name, $packageNameVersion->version);
56
        $bowerphp = $this->getBowerphp($output);
57
58
        $url = $bowerphp->getPackageInfo($package);
59
60
        $default = $this->getDefaultBrowser();
61
62
        $arg = "$default \"$url\"";
63
64
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
65
            $output->writeln($arg);
66
        } else {
67
            $output->writeln('');
68
        }
69
        // @codeCoverageIgnoreStart
70
        if (!defined('PHPUNIT_BOWER_TESTSUITE')) {
71
            $browser = new Process($arg);
72
            $browser->start();
73
            while ($browser->isRunning()) {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
74
                // do nothing...
75
            }
76
        }
77
        // @codeCoverageIgnoreEnd
78
    }
79
80
    /**
81
     * @return string
82
     * @codeCoverageIgnore
83
     */
84
    private function getDefaultBrowser()
85
    {
86
        $xdgOpen = new Process('which xdg-open');
87
        $xdgOpen->run();
88
        if (!$xdgOpen->isSuccessful()) {
89
            $open = new Process('which open');
90
            $open->run();
91
            if (!$open->isSuccessful()) {
92
                throw new \RuntimeException('Could not open default browser.');
93
            }
94
95
            return trim($open->getOutput());
96
        }
97
98
        return trim($xdgOpen->getOutput());
99
    }
100
}
101