InitHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 0 Features 2
Metric Value
dl 0
loc 95
rs 10
c 7
b 0
f 2
wmc 5
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B handle() 0 53 4
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 Hogosha\Monitor\Configuration\ConfigurationDumper;
19
use Hogosha\Monitor\Configuration\ConfigurationLoader;
20
use Hogosha\Monitor\DependencyInjection\Exception\ConfigurationLoadingException;
21
use Hogosha\Monitor\Monitor;
22
use Symfony\Component\Filesystem\Filesystem;
23
use Webmozart\Console\Api\Args\Args;
24
use Webmozart\Console\Api\IO\IO;
25
26
/**
27
 * @author Guillaume Cavana <[email protected]>
28
 */
29
class InitHandler
30
{
31
    /**
32
     * $configurationLoader.
33
     *
34
     * @var ConfigurationLoader
35
     */
36
    protected $configurationLoader;
37
38
    /**
39
     * $configurationDumper.
40
     *
41
     * @var ConfigurationDumper
42
     */
43
    protected $configurationDumper;
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param ConfigurationLoader $configurationLoader
49
     * @param ConfigurationDumper $configurationDumper
50
     * @param Filesystem          $filesystem
51
     */
52
    public function __construct(
53
        ConfigurationLoader $configurationLoader,
54
        ConfigurationDumper $configurationDumper,
55
        Filesystem $filesystem
56
    ) {
57
        $this->configurationLoader = $configurationLoader;
58
        $this->configurationDumper = $configurationDumper;
59
        $this->filesystem = $filesystem;
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
    }
61
62
    /**
63
     * handle.
64
     *
65
     * @param Args $args
66
     * @param IO   $io
67
     *
68
     * @return int
69
     */
70
    public function handle(Args $args, IO $io)
71
    {
72
        $configFileExist = true;
73
        $overwrite = is_string($args->getOption('force'));
74
75
        try {
76
            $this->configurationLoader->setRootDirectory($args->getOption('config'));
77
            $configuration = $this->configurationLoader->loadConfiguration();
0 ignored issues
show
Unused Code introduced by
$configuration is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
78
        } catch (ConfigurationLoadingException $e) {
79
            $configFileExist = false;
80
        }
81
82
        if (!$configFileExist || $overwrite) {
83
            $configuration = [
84
                'urls' => [
85
                    'google' => [
86
                        'url' => 'https://www.google.fr',
87
                        'method' => 'GET',
88
                        'headers' => [],
89
                        'timeout' => 1,
90
                        'validator' => [],
91
                        'status_code' => 200,
92
                        'metric_uuid' => null,
93
                        'service_uuid' => null,
94
                    ],
95
                ],
96
                'hogosha_portal' => [
97
                    'username' => '',
98
                    'password' => '',
99
                    'base_uri' => 'http://localhost:8000/api/',
100
                    'metric_update' => false,
101
                    'incident_update' => false,
102
                    'default_failed_incident_message' => 'An error as occured, we are investigating %service_name%',
103
                    'default_resolved_incident_message' => 'The service %service_name% is back to normal',
104
                ],
105
            ];
106
107
            // Dump configuration
108
            $content = $this->configurationDumper->dumpConfiguration($configuration);
109
            $this->filesystem->dumpFile(
110
                $this->configurationLoader->getConfigurationFilepath(),
111
                $content
112
            );
113
            $io->writeLine('<info>Creating monitor file</info>');
114
        } else {
115
            $io->writeLine(
116
                sprintf(
117
                    '<info>You already have a configuration file in</info> "%s"',
118
                    $this->configurationLoader->getConfigurationFilepath()
119
                )
120
            );
121
        }
122
    }
123
}
124