Passed
Branch new-architecture (05bfee)
by James
04:16
created

DetectCommand::execute()   C

Complexity

Conditions 7
Paths 14

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 33
ccs 29
cts 29
cp 1
rs 6.7272
cc 7
eloc 23
nc 14
nop 2
crap 7
1
<?php
2
3
/**
4
 * This file is, guess what, part of WebHelper.
5
 *
6
 * (c) James <[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 JamesRezo\WebHelper\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Input\InputArgument;
18
use JamesRezo\WebHelper\Factory;
19
20
/**
21
 * Detects webservers.
22
 */
23
class DetectCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 3
    protected function configure()
29
    {
30 3
        $this
31 3
            ->setName('detect')
32 3
            ->setDescription('Detect webservers on this platform.')
33 3
            ->setHelp('The <info>detect</info> command finds webserver binaries in the PATH'.
34 3
                ' or in the path passed in argument.')
35 3
            ->addArgument('path', InputArgument::IS_ARRAY, 'path list to look into for a binary instead of PATH.')
36
        ;
37 3
    }
38
39
    /**
40
     * Execute the command.
41
     *
42
     * {@inheritdoc}
43
     *
44
     * @param InputInterface  $input  the input interface
45
     * @param OutputInterface $output the output interface
46
     */
47 2
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49 2
        $path = $input->getArgument('path');
50 2
        if (empty($path)) {
51 1
            $path = explode(PATH_SEPARATOR, getenv('PATH'));
52 1
        }
53
54 2
        $factory = new Factory();
55 2
        foreach ($factory->getKnownWebServers() as $webserver) {
56 2
            $wsObject = $factory->createWebServer($webserver);
57 2
            foreach ($wsObject->getBinaries() as $binary) {
58 2
                $file = $this->getFromPath($binary, $path);
59 2
                if ($file) {
60 1
                    $output->writeln('<comment>'.$file.' detected for '.$webserver.'.</comment>');
61 1
                    $settings = $wsObject->getSettings($file);
62 1
                    $version = $wsObject->extractVersion($settings);
63 1
                    $configFile = $wsObject->extractRootConfigurationFile($settings);
64 1
                    if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
65 1
                        $output->write($settings);
66 1
                    }
67 1
                    $output->writeln('<info>Detected version:</info>'.$version);
68 1
                    $output->writeln('<info>Detected config file:</info>'.$configFile);
69 1
                    if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
70 1
                        $output->writeln(
71
                            '<comment>You should analyze this with </comment>'.
72 1
                            'bin/wh analyze '.$webserver.':'.$version.' '.$configFile
73 1
                        );
74 1
                    }
75 1
                    break;
76
                }
77 2
            }
78 2
        }
79 2
    }
80
81 2
    private function getFromPath($binary, array $path = array())
82
    {
83 2
        $file = '';
84
85 2
        foreach ($path as $loop) {
86 2
            $loop = preg_replace('#('.preg_quote(DIRECTORY_SEPARATOR).')*$#', '', $loop).DIRECTORY_SEPARATOR;
87 2
            if (is_executable($loop.$binary)) {
88 1
                $file = $loop.$binary;
89 1
            }
90 2
        }
91
92 2
        return $file;
93
    }
94
}
95