RunHandler::handle()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
cc 5
eloc 25
c 6
b 0
f 2
nc 16
nop 2
dl 0
loc 39
rs 8.439
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Console\Handler;
17
18
use GuzzleHttp\Exception\BadResponseException;
19
use Hogosha\Monitor\Client\GuzzleClient;
20
use Hogosha\Monitor\Configuration\ConfigurationLoader;
21
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException;
22
use Hogosha\Monitor\Model\UrlProvider;
23
use Hogosha\Monitor\Pusher\PusherManager;
24
use Hogosha\Monitor\Renderer\RendererFactory;
25
use Hogosha\Monitor\Runner\Runner;
26
use Webmozart\Console\Api\Args\Args;
27
use Webmozart\Console\Api\IO\IO;
28
29
/**
30
 * @author Guillaume Cavana <[email protected]>
31
 */
32
class RunHandler
33
{
34
    /**
35
     * $configurationLoader.
36
     *
37
     * @var ConfigurationLoader
38
     */
39
    protected $configurationLoader;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param ConfigurationLoader $configurationLoader
45
     */
46
    public function __construct(ConfigurationLoader $configurationLoader)
47
    {
48
        $this->configurationLoader = $configurationLoader;
49
    }
50
51
    /**
52
     * handle.
53
     *
54
     * @param Args $args
55
     * @param IO   $io
56
     *
57
     * @return int
58
     */
59
    public function handle(Args $args, IO $io)
60
    {
61
        $this->configurationLoader->setRootDirectory($args->getOption('config'));
62
63
        $config = $this->configurationLoader->loadConfiguration();
64
65
        try {
66
            $urlProvider = new UrlProvider($config);
67
            $runner = new Runner(
68
                $urlProvider,
69
                GuzzleClient::createClient($io)
70
            );
71
72
            $renderer = RendererFactory::create($args->getOption('format'), $io);
73
            $results = $runner->run();
74
            $renderer->render($results);
75
76
            if (isset($config['hogosha_portal']['metric_update']) ||
77
                isset($config['hogosha_portal']['incident_update'])
78
            ) {
79
                $pusher = new PusherManager($config, $io);
80
                $pusher->push($results);
81
            }
82
        } catch (BadResponseException $e) {
83
            $io->writeLine(
84
                sprintf(
85
                    '<error>Exception:</error> "%s"',
86
                    $e->getMessage()
87
                )
88
            );
89
        } catch (ConfigurationLoadingException $e) {
90
            $io->writeLine(
91
                sprintf(
92
                    '<info>There is no configuration file in</info> "%s"',
93
                    $this->configurationLoader->getRootDirectory()
94
                )
95
            );
96
        }
97
    }
98
}
99