Completed
Push — master ( d7d231...3a9895 )
by Nils
06:20
created

SmokeCommand::getHttpClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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