Completed
Push — master ( ad0611...abd676 )
by Tom
09:08 queued 04:30
created

OpenBrowserCommand::execute()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
rs 4.909
c 1
b 0
f 0
cc 9
eloc 24
nc 24
nop 2
1
<?php
2
3
namespace N98\Magento\Command;
4
5
use Magento\Backend\Model\UrlInterface as BackendUrlInterface;
6
use Magento\Framework\UrlInterface as FrontendUrlInterface;
7
use Magento\Store\Api\Data\StoreInterface;
8
use Magento\Store\Model\Store;
9
use RuntimeException;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use N98\Util\OperatingSystem;
14
use N98\Util\Exec;
15
16
class OpenBrowserCommand extends AbstractMagentoCommand
17
{
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('open-browser')
22
            ->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID')
23
            ->setDescription('Open current project in browser <comment>(experimental)</comment>')
24
        ;
25
    }
26
27
    /**
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     * @throws RuntimeException
31
     * @return int|void
32
     */
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $opener = '';
36
        if (OperatingSystem::isMacOs()) {
37
            $opener = 'open';
38
        } elseif (OperatingSystem::isWindows()) {
39
            $opener = 'start';
40
        } else {
41
            // Linux
42
            if (exec('which xde-open')) {
43
                $opener = 'xdg-open';
44
            } elseif (exec('which gnome-open')) {
45
                $opener = 'gnome-open';
46
            } elseif (exec('which kde-open')) {
47
                $opener = 'kde-open';
48
            }
49
        }
50
51
        if (empty($opener)) {
52
            throw new RuntimeException('No opener command like xde-open, gnome-open, kde-open was found.');
53
        }
54
55
        $this->detectMagento($output);
56
        if ($this->initMagento($output)) {
0 ignored issues
show
Unused Code introduced by
The call to OpenBrowserCommand::initMagento() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
57
            $store = $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', true);
58
59
            if ($store->getId() == Store::DEFAULT_STORE_ID) {
60
                $url = $this->getBackendStoreUrl($store);
61
            } else {
62
                $url = $this->getFrontendStoreUrl($store);
63
            }
64
65
            $output->writeln('Opening URL <comment>' . $url . '</comment> in browser');
66
67
            Exec::run(escapeshellcmd($opener . ' ' . $url));
68
        }
69
    }
70
71
    /**
72
     * @param StoreInterface $store
73
     * @return string
74
     */
75
    private function getBackendStoreUrl(StoreInterface $store)
76
    {
77
        $baseConfig = $this->getHelper('magento')->getBaseConfig();
78
79
        if (!isset($baseConfig['backend']['frontName'])) {
80
            throw new RuntimeException('frontName for admin area could not be found.');
81
        }
82
        $adminFrontName = $baseConfig['backend']['frontName'];
83
84
        return rtrim($store->getBaseUrl(BackendUrlInterface::URL_TYPE_WEB), '/') . '/' . $adminFrontName;
85
    }
86
87
    /**
88
     * @param StoreInterface $store
89
     * @return string
90
     */
91
    private function getFrontendStoreUrl(StoreInterface $store)
92
    {
93
        return $store->getBaseUrl(FrontendUrlInterface::URL_TYPE_LINK) . '?___store=' . $store->getCode();
94
    }
95
}
96