Completed
Push — master ( 540363...880b7a )
by Tom
12:04 queued 07:25
created

OpenBrowserCommand::resolveOpenerCommand()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 31
rs 5.3846
cc 8
eloc 20
nc 18
nop 1
1
<?php
2
3
namespace N98\Magento\Command;
4
5
use N98\Util\Exec;
6
use N98\Util\OperatingSystem;
7
use RuntimeException;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class OpenBrowserCommand extends AbstractMagentoCommand
13
{
14
    protected function configure()
15
    {
16
        $this
17
            ->setName('open-browser')
18
            ->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID')
19
            ->setDescription('Open current project in browser <comment>(experimental)</comment>')
20
        ;
21
    }
22
23
    /**
24
     * @return bool
25
     */
26
    public function isEnabled()
27
    {
28
        return Exec::allowed();
29
    }
30
31
    /**
32
     * @param InputInterface $input
33
     * @param OutputInterface $output
34
     * @throws RuntimeException
35
     * @return int|void
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->detectMagento($output);
40
        if (!$this->initMagento()) {
41
            return;
42
        }
43
44
        $store = $this->getHelper('parameter')->askStore($input, $output, 'store', true);
45
        if ($store->getId() == \Mage_Core_Model_App::ADMIN_STORE_ID) {
46
            $adminFrontName = (string) \Mage::getConfig()->getNode('admin/routers/adminhtml/args/frontName');
47
            $url = rtrim($store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_WEB), '/') . '/' . $adminFrontName;
48
        } else {
49
            $url = $store->getBaseUrl(\Mage_Core_Model_Store::URL_TYPE_LINK) . '?___store=' . $store->getCode();
50
        }
51
        $output->writeln('Opening URL <comment>' . $url . '</comment> in browser');
52
53
        $opener = $this->resolveOpenerCommand($output);
54
        Exec::run(escapeshellcmd($opener . ' ' . $url));
55
    }
56
57
    /**
58
     * @param OutputInterface $output
59
     * @return string
60
     */
61
    private function resolveOpenerCommand(OutputInterface $output)
62
    {
63
        $opener = '';
64
        if (OperatingSystem::isMacOs()) {
65
            $opener = 'open';
66
        } elseif (OperatingSystem::isWindows()) {
67
            $opener = 'start';
68
        } else {
69
            // Linux
70
            if (exec('which xdg-open')) {
71
                $opener = 'xdg-open';
72
            } elseif (exec('which gnome-open')) {
73
                $opener = 'gnome-open';
74
            } elseif (exec('which kde-open')) {
75
                $opener = 'kde-open';
76
            }
77
        }
78
79
        if (empty($opener)) {
80
            throw new RuntimeException('No opener command like xdg-open, gnome-open, kde-open was found.');
81
        }
82
83
        if (OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity()) {
84
            $message = sprintf('open command is "%s"', $opener);
85
            $output->writeln(
86
                '<debug>' . $message . '</debug>'
87
            );
88
        }
89
90
        return $opener;
91
    }
92
}
93