Completed
Push — master ( 023e21...5ec956 )
by Guillaume
07:09
created

RunHandler::handle()   B

Complexity

Conditions 2
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 24
rs 8.9714
cc 2
eloc 14
nc 5
nop 2
1
<?php
2
3
namespace Hogosha\Monitor\Console\Handler;
4
5
use Hogosha\Monitor\Client\GuzzleClient;
6
use Hogosha\Monitor\Configuration\ConfigurationLoader;
7
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException;
8
use Hogosha\Monitor\Model\UrlProvider;
9
use Hogosha\Monitor\Renderer\RendererFactory;
10
use Hogosha\Monitor\Runner\Runner;
11
use Webmozart\Console\Api\Args\Args;
12
use Webmozart\Console\Api\IO\IO;
13
14
/**
15
 * @author Guillaume Cavana <[email protected]>
16
 */
17
class RunHandler
18
{
19
    /**
20
     * $configurationLoader.
21
     *
22
     * @var ConfigurationLoader
23
     */
24
    protected $configurationLoader;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param ConfigurationLoader $configurationLoader
30
     */
31
    public function __construct(ConfigurationLoader $configurationLoader)
32
    {
33
        $this->configurationLoader = $configurationLoader;
34
    }
35
36
    /**
37
     * handle.
38
     *
39
     * @param Args $args
40
     * @param IO   $io
41
     *
42
     * @return int
43
     */
44
    public function handle(Args $args, IO $io)
45
    {
46
        $this->configurationLoader->setRootDirectory($args->getOption('config'));
47
48
        try {
49
50
            $runner = new Runner(
51
                new UrlProvider($this->configurationLoader),
52
                GuzzleClient::createClient()
53
            );
54
55
            $renderer = RendererFactory::create($args->getOption('format'), $io);
56
            $results = $runner->run();
57
            $renderer->render($results);
58
59
        } catch (ConfigurationLoadingException $e) {
60
            $io->writeLine(
61
                sprintf(
62
                    '<info>There is no configuration file in</info> "%s"',
63
                    $this->configurationLoader->getRootDirectory()
64
                )
65
            );
66
        }
67
    }
68
}
69