Completed
Push — master ( 33a55e...ca53a0 )
by Nils
07:47
created

SmokeCommand::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 2
nop 3
1
<?php
2
3
namespace whm\Smoke\Cli\Command;
4
5
use Cache\Adapter\Filesystem\FilesystemCachePool;
6
use Ivory\HttpAdapter\CurlHttpAdapter;
7
use League\Flysystem\Adapter\Local;
8
use League\Flysystem\Filesystem;
9
use phm\HttpWebdriverClient\Http\Client\Chrome\ChromeClient;
10
use phm\HttpWebdriverClient\Http\Client\Decorator\CacheDecorator;
11
use phm\HttpWebdriverClient\Http\Client\Guzzle\GuzzleClient;
12
use phm\HttpWebdriverClient\Http\Client\HttpClient;
13
use phmLabs\Components\Annovent\Dispatcher;
14
use PhmLabs\Components\Init\Init;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use whm\Smoke\Config\Configuration;
19
use whm\Smoke\Extensions\SmokeHttpClient\CacheAware;
20
use whm\Smoke\Scanner\Scanner;
21
use whm\Smoke\Yaml\EnvAwareYaml;
22
23
class SmokeCommand extends Command
24
{
25
    /**
26
     * @var OutputInterface
27
     */
28
    protected $output;
29
    protected $eventDispatcher;
30
31
    /**
32
     * @var Configuration
33
     */
34
    protected $config;
35
36
    protected function init(InputInterface $input, OutputInterface $output, $url = null)
37
    {
38
        if ($input->hasOption('bootstrap') && !is_null($input->getOption('bootstrap'))) {
39
            include $input->getOption('bootstrap');
40
        }
41
42
        $this->output = $output;
43
        $this->eventDispatcher = new Dispatcher();
44
45
        Init::registerGlobalParameter('_eventDispatcher', $this->eventDispatcher);
46
        Init::registerGlobalParameter('_output', $output);
47
48
        $this->writeSmokeCredentials($url);
49
    }
50
51
    /**
52
     * This function creates the credentials header for the command line.
53
     *
54
     * @param null $url
55
     */
56
    protected function writeSmokeCredentials($url = null)
57
    {
58
        $this->output->writeln("\n Smoke " . SMOKE_VERSION . " by Nils Langner\n");
59
60
        if ($url) {
61
            $this->output->writeln(' <info>Scanning ' . $url . "</info>\n");
62
        }
63
    }
64
65
    protected function scan()
66
    {
67
        $scanner = new Scanner($this->config->getRules(),
68
            $this->config->getClient(),
69
            $this->eventDispatcher,
70
            $this->config->getExtension('_ResponseRetriever')->getRetriever());
71
72
        $scanner->scan();
73
74
        return $scanner->getStatus();
75
    }
76
77
    /**
78
     * Returns an array representing the configuration.
79
     *
80
     * @param $configFile
81
     *
82
     * @return array
83
     */
84
    protected function getConfigArray($configFile, $mandatory = false)
85
    {
86
        $configArray = array();
87
88
        if ($configFile) {
89
            if (strpos($configFile, 'http://') === 0 || strpos($configFile, 'https://') === 0) {
90
                $curlClient = new CurlHttpAdapter();
91
                $fileContent = (string)$curlClient->get($configFile)->getBody();
92
            } else {
93
                if (file_exists($configFile)) {
94
                    $fileContent = file_get_contents($configFile);
95
                } else {
96
                    throw new \RuntimeException("Config file was not found ('" . $configFile . "').");
97
                }
98
            }
99
            $configArray = EnvAwareYaml::parse($fileContent);
100
        } else {
101
            if ($mandatory) {
102
                throw new \RuntimeException('Config file was not defined.');
103
            }
104
        }
105
106
        return $configArray;
107
    }
108
}
109