Completed
Branch master (1f9106)
by Nils
02:44
created

SmokeCommand::init()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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 Ivory\HttpAdapter\CurlHttpAdapter;
6
use Ivory\HttpAdapter\Event\Subscriber\RedirectSubscriber;
7
use Ivory\HttpAdapter\Event\Subscriber\RetrySubscriber;
8
use Ivory\HttpAdapter\EventDispatcherHttpAdapter;
9
use Ivory\HttpAdapter\HttpAdapterFactory;
10
use phmLabs\Components\Annovent\Dispatcher;
11
use PhmLabs\Components\Init\Init;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
use whm\Smoke\Http\MessageFactory;
17
use whm\Smoke\Scanner\Scanner;
18
use whm\Smoke\Yaml\EnvAwareYaml;
19
20
class SmokeCommand extends Command
21
{
22
    protected $output;
23
    protected $eventDispatcher;
24
    protected $config;
25
26
    protected function init(InputInterface $input, OutputInterface $output, $url = null)
27
    {
28
        if ($input->hasOption('bootstrap') && !is_null($input->getOption('bootstrap'))) {
29
            include $input->getOption('bootstrap');
30
        }
31
32
        $this->output = $output;
33
        $this->eventDispatcher = new Dispatcher();
34
35
        Init::registerGlobalParameter('_eventDispatcher', $this->eventDispatcher);
36
        Init::registerGlobalParameter('_output', $output);
37
38
        $this->writeSmokeCredentials($url);
39
    }
40
41
    /**
42
     * This function creates the credentials header for the command line.
43
     *
44
     * @param null $url
45
     */
46
    protected function writeSmokeCredentials($url = null)
47
    {
48
        $this->output->writeln("\n Smoke " . SMOKE_VERSION . " by Nils Langner\n");
49
50
        if ($url) {
51
            $this->output->writeln(' <info>Scanning ' . $url . "</info>\n");
52
        }
53
    }
54
55
    /**
56
     * This function return a http client.
57
     *
58
     * @throws \Ivory\HttpAdapter\HttpAdapterException
59
     *
60
     * @return \Ivory\HttpAdapter\HttpAdapterInterface
61
     */
62
    protected function getHttpClient()
63
    {
64
        $eventDispatcher = new EventDispatcher();
65
        $eventDispatcher->addSubscriber(new RedirectSubscriber());
66
        $eventDispatcher->addSubscriber(new RetrySubscriber());
67
68
        // $guessedAdapter = HttpAdapterFactory::guess();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
69
        /** @var \Ivory\HttpAdapter\Guzzle6HttpAdapter $guessedAdapter */
70
        $guessedAdapter = new CurlHttpAdapter();
71
72
        $adapter = new EventDispatcherHttpAdapter($guessedAdapter, $eventDispatcher);
73
        $adapter->getConfiguration()->setTimeout(30);
74
        //$adapter->getConfiguration()->setUserAgent('versioneye-php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
75
        $adapter->getConfiguration()->setMessageFactory(new MessageFactory());
76
77
        return $adapter;
78
    }
79
80
    protected function scan()
81
    {
82
        $scanner = new Scanner($this->config->getRules(),
83
            $this->getHttpClient(),
84
            $this->eventDispatcher,
85
            $this->config->getExtension('_ResponseRetriever')->getRetriever());
86
87
        $scanner->scan();
88
89
        return $scanner->getStatus();
90
    }
91
92
    /**
93
     * Returns an array representing the configuration.
94
     *
95
     * @param $configFile
96
     *
97
     * @return array
98
     */
99
    protected function getConfigArray($configFile, $mandatory = false)
100
    {
101
        $configArray = array();
102
103
        if ($configFile) {
104
            if (strpos($configFile, 'http://') === 0) {
105
                $fileContent = (string) $this->getHttpClient()->get($configFile)->getBody();
106
            } else {
107
                if (file_exists($configFile)) {
108
                    $fileContent = file_get_contents($configFile);
109
                } else {
110
                    throw new \RuntimeException("Config file was not found ('" . $configFile . "').");
111
                }
112
            }
113
            $configArray = EnvAwareYaml::parse($fileContent);
114
        } else {
115
            if ($mandatory) {
116
                throw new \RuntimeException('Config file was not defined.');
117
            }
118
        }
119
120
        return $configArray;
121
    }
122
}
123