SmokeCommand   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 10
dl 0
loc 107
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 14 3
A writeSmokeCredentials() 0 12 3
A scan() 0 24 4
B getConfigArray() 0 24 6
1
<?php
2
3
namespace whm\Smoke\Cli\Command;
4
5
use GuzzleHttp\Client;
6
use phmLabs\Components\Annovent\Dispatcher;
7
use PhmLabs\Components\Init\Init;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use whm\Smoke\Config\Configuration;
12
use whm\Smoke\Scanner\Scanner;
13
use whm\Smoke\Yaml\EnvAwareYaml;
14
15
class SmokeCommand extends Command
16
{
17
    /**
18
     * @var OutputInterface
19
     */
20
    protected $output;
21
    protected $eventDispatcher;
22
23
    /**
24
     * @var Configuration
25
     */
26
    protected $config;
27
28
    protected function init(InputInterface $input, OutputInterface $output, $url = null)
29
    {
30
        if ($input->hasOption('bootstrap') && !is_null($input->getOption('bootstrap'))) {
31
            include $input->getOption('bootstrap');
32
        }
33
34
        $this->output = $output;
35
        $this->eventDispatcher = new Dispatcher();
36
37
        Init::registerGlobalParameter('_eventDispatcher', $this->eventDispatcher);
38
        Init::registerGlobalParameter('_output', $output);
39
40
        $this->writeSmokeCredentials($url);
41
    }
42
43
    /**
44
     * This function creates the credentials header for the command line.
45
     *
46
     * @param null $url
47
     */
48
    protected function writeSmokeCredentials($url = null)
49
    {
50
        if (defined('SMOKE_CREDENTIALS')) {
51
            $this->output->writeln("\n " . SMOKE_CREDENTIALS . "\n");
52
        } else {
53
            $this->output->writeln("\n Smoke " . SMOKE_VERSION . " by Nils Langner\n");
54
        }
55
56
        if ($url) {
57
            $this->output->writeln(' <info>Scanning ' . $url . "</info>\n");
58
        }
59
    }
60
61
    /**
62
     * @return int
63
     * @throws \Exception
64
     */
65
    protected function scan()
66
    {
67
        $client = $this->config->getClient();
68
69
        $scanner = new Scanner($this->config->getRules(),
70
            $client,
71
            $this->eventDispatcher,
72
            $this->config->getExtension('_ResponseRetriever')->getRetriever());
73
74
        try {
75
            $scanner->scan();
76
        } catch (\Exception $e) {
77
            if (method_exists($client, 'close')) {
78
                $client->close();
79
            }
80
            throw $e;
81
        }
82
83
        if (method_exists($client, 'close')) {
84
            $client->close();
85
        }
86
87
        return $scanner->getStatus();
88
    }
89
90
    /**
91
     * Returns an array representing the configuration.
92
     *
93
     * @param $configFile
94
     *
95
     * @return array
96
     */
97
    protected function getConfigArray($configFile, $mandatory = false)
98
    {
99
        $configArray = array();
100
101
        if ($configFile) {
102
            if (strpos($configFile, 'http://') === 0 || strpos($configFile, 'https://') === 0) {
103
                $curlClient = new Client();
104
                $fileContent = (string)$curlClient->get($configFile)->getBody();
105
            } else {
106
                if (file_exists($configFile)) {
107
                    $fileContent = file_get_contents($configFile);
108
                } else {
109
                    throw new \RuntimeException("Config file was not found ('" . $configFile . "').");
110
                }
111
            }
112
            $configArray = EnvAwareYaml::parse($fileContent);
113
        } else {
114
            if ($mandatory) {
115
                throw new \RuntimeException('Config file was not defined.');
116
            }
117
        }
118
119
        return $configArray;
120
    }
121
}
122