Completed
Push — master ( 40aedc...7c1b35 )
by Nils
05:00
created

SmokeCommand   C

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 22

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
c 2
b 0
f 0
lcom 1
cbo 22
dl 0
loc 126
rs 5.7894

5 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 14 3
A writeSmokeCredentials() 0 8 2
B getHttpClient() 0 24 1
A scan() 0 11 1
C getConfigArray() 0 33 8
1
<?php
2
3
namespace whm\Smoke\Cli\Command;
4
5
use Cache\Adapter\Filesystem\FilesystemCachePool;
6
use Ivory\HttpAdapter\CurlHttpAdapter;
7
use Ivory\HttpAdapter\Event\Subscriber\RedirectSubscriber;
8
use Ivory\HttpAdapter\Event\Subscriber\RetrySubscriber;
9
use Ivory\HttpAdapter\EventDispatcherHttpAdapter;
10
use League\Flysystem\Adapter\Local;
11
use League\Flysystem\Filesystem;
12
use phmLabs\Components\Annovent\Dispatcher;
13
use PhmLabs\Components\Init\Init;
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\EventDispatcher\EventDispatcher;
18
use whm\Crawler\Http\RequestFactory;
19
use whm\Smoke\Config\Configuration;
20
use whm\Smoke\Extensions\SmokeHttpClient\CacheAware;
21
use whm\Smoke\Http\CacheDecorator;
22
use whm\Smoke\Http\MessageFactory;
23
use whm\Smoke\Scanner\Scanner;
24
use whm\Smoke\Yaml\EnvAwareYaml;
25
26
class SmokeCommand extends Command
27
{
28
    /**
29
     * @var OutputInterface
30
     */
31
    protected $output;
32
    protected $eventDispatcher;
33
34
    /**
35
     * @var Configuration
36
     */
37
    protected $config;
38
39
    protected function init(InputInterface $input, OutputInterface $output, $url = null)
40
    {
41
        if ($input->hasOption('bootstrap') && !is_null($input->getOption('bootstrap'))) {
42
            include $input->getOption('bootstrap');
43
        }
44
45
        $this->output = $output;
46
        $this->eventDispatcher = new Dispatcher();
47
48
        Init::registerGlobalParameter('_eventDispatcher', $this->eventDispatcher);
49
        Init::registerGlobalParameter('_output', $output);
50
51
        $this->writeSmokeCredentials($url);
52
    }
53
54
    /**
55
     * This function creates the credentials header for the command line.
56
     *
57
     * @param null $url
58
     */
59
    protected function writeSmokeCredentials($url = null)
60
    {
61
        $this->output->writeln("\n Smoke " . SMOKE_VERSION . " by Nils Langner\n");
62
63
        if ($url) {
64
            $this->output->writeln(' <info>Scanning ' . $url . "</info>\n");
65
        }
66
    }
67
68
    /**
69
     * This function return a http client.
70
     *
71
     * @throws \Ivory\HttpAdapter\HttpAdapterException
72
     * @return \Ivory\HttpAdapter\HttpAdapterInterface
73
     */
74
    protected function getHttpClient()
75
    {
76
        $eventDispatcher = new EventDispatcher();
77
        $eventDispatcher->addSubscriber(new RedirectSubscriber());
78
        $eventDispatcher->addSubscriber(new RetrySubscriber());
79
80
        $guessedAdapter = new CurlHttpAdapter();
81
82
        RequestFactory::addStandardHeader('Accept-Encoding', 'gzip');
83
        RequestFactory::addStandardHeader('Connection', 'keep-alive');
84
85
        $adapter = new EventDispatcherHttpAdapter($guessedAdapter, $eventDispatcher);
86
        $adapter->getConfiguration()->setTimeout(30);
87
        $adapter->getConfiguration()->setMessageFactory(new MessageFactory());
88
89
        // add cache handling to adapter
90
        // @todo create an extension
91
        $filesystemAdapter = new Local('/tmp/cached/');
92
        $filesystem = new Filesystem($filesystemAdapter);
93
        $cachePoolInterface = new FilesystemCachePool($filesystem);
94
        $cachedAdapter = new CacheDecorator($adapter, $cachePoolInterface);
95
96
        return $cachedAdapter;
97
    }
98
99
    protected function scan()
100
    {
101
        $scanner = new Scanner($this->config->getRules(),
102
            $this->getHttpClient(),
103
            $this->eventDispatcher,
104
            $this->config->getExtension('_ResponseRetriever')->getRetriever());
105
106
        $scanner->scan();
107
108
        return $scanner->getStatus();
109
    }
110
111
    /**
112
     * Returns an array representing the configuration.
113
     *
114
     * @param $configFile
115
     *
116
     * @return array
117
     */
118
    protected function getConfigArray($configFile, $mandatory = false)
119
    {
120
        $configArray = array();
121
122
        if ($configFile) {
123
            if (strpos($configFile, 'http://') === 0 || strpos($configFile, 'https://') === 0) {
124
                $httpClient = $this->getHttpClient();
125
126
                if ($httpClient instanceof CacheAware) {
127
                    $httpClient->disableCache();
128
                }
129
130
                $fileContent = (string)$httpClient->get($configFile)->getBody();
131
132
                if ($httpClient instanceof CacheAware) {
133
                    $httpClient->enableCache();
134
                }
135
            } else {
136
                if (file_exists($configFile)) {
137
                    $fileContent = file_get_contents($configFile);
138
                } else {
139
                    throw new \RuntimeException("Config file was not found ('" . $configFile . "').");
140
                }
141
            }
142
            $configArray = EnvAwareYaml::parse($fileContent);
143
        } else {
144
            if ($mandatory) {
145
                throw new \RuntimeException('Config file was not defined.');
146
            }
147
        }
148
149
        return $configArray;
150
    }
151
}
152