Passed
Push — master ( 146e66...93ff04 )
by Nils
02:21
created

RunCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 98
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initFormat() 0 9 2
A initHealthFoundation() 0 29 5
A configure() 0 9 1
A initMessages() 0 18 5
A execute() 0 18 2
1
<?php
2
3
namespace Leankoala\HealthFoundation\Cli\Command;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\HealthFoundation;
7
use Leankoala\HealthFoundation\Result\Format\Ietf\IetfFormat;
8
use PhmLabs\Components\Init\Init;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Yaml\Yaml;
14
15
16
class RunCommand extends Command
17
{
18
    protected function configure()
19
    {
20
        $this
21
            ->setDefinition([
22
                new InputArgument('config', InputArgument::REQUIRED, 'the configuration file')
23
            ])
24
            ->setDescription('Run health checks.')
25
            ->setHelp('The <info>run</info> command for the health checker.')
26
            ->setName('run');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $configFile = $input->getArgument('config');
32
33
        if (!file_exists($configFile)) {
0 ignored issues
show
Bug introduced by
It seems like $configFile can also be of type string[]; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        if (!file_exists(/** @scrutinizer ignore-type */ $configFile)) {
Loading history...
34
            throw new \RuntimeException('Unable to find config file.');
35
        }
36
37
        $configArray = Yaml::parse(file_get_contents($configFile));
0 ignored issues
show
Bug introduced by
It seems like $configFile can also be of type string[]; however, parameter $filename of file_get_contents() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $configArray = Yaml::parse(file_get_contents(/** @scrutinizer ignore-type */ $configFile));
Loading history...
38
39
        $format = $this->initFormat($configArray);
40
        $messages = $this->initMessages($configArray);
41
42
        $healthFoundation = $this->initHealthFoundation($configArray);
43
44
        $runResult = $healthFoundation->runHealthCheck();
45
46
        $format->handle($runResult, $messages['success'], $messages['failure']);
47
    }
48
49
    /**
50
     * @param array $configArray
51
     *
52
     * @return HealthFoundation
53
     */
54
    private function initHealthFoundation($configArray)
55
    {
56
        if (!array_key_exists('checks', $configArray)) {
57
            throw new \RuntimeException('The mandatory config element "checks" is missing.');
58
        }
59
60
        $healthFoundation = new HealthFoundation();
61
62
        foreach ($configArray['checks'] as $key => $checkArray) {
63
64
            /** @var Check $check */
65
            $check = Init::initialize($checkArray, 'check');
0 ignored issues
show
Unused Code introduced by
The call to PhmLabs\Components\Init\Init::initialize() has too many arguments starting with 'check'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            /** @scrutinizer ignore-call */ 
66
            $check = Init::initialize($checkArray, 'check');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
67
            if (array_key_exists('description', $checkArray)) {
68
                $description = $checkArray['description'];
69
            } else {
70
                $description = "";
71
            }
72
73
            if (array_key_exists('identifier', $checkArray)) {
74
                $identifier = $checkArray['identifier'];
75
            } else {
76
                $identifier = $key;
77
            }
78
79
            $healthFoundation->registerCheck($check, $identifier, $description);
80
        }
81
82
        return $healthFoundation;
83
    }
84
85
    private function initMessages($configArray)
86
    {
87
        $successMessage = 'The health check was passed.';
88
        $failureMessage = 'The health check failed';
89
90
        if (array_key_exists('foundation', $configArray)) {
91
            if (array_key_exists('messages', $configArray['foundation'])) {
92
                if (array_key_exists('success', $configArray['foundation']['messages'])) {
93
                    $successMessage = $configArray['foundation']['messages']['success'];
94
                }
95
96
                if (array_key_exists('failure', $configArray['foundation']['messages'])) {
97
                    $failureMessage = $configArray['foundation']['messages']['failure'];
98
                }
99
            }
100
        }
101
102
        return ['success' => $successMessage, 'failure' => $failureMessage];
103
    }
104
105
    private function initFormat($configArray)
106
    {
107
        if (array_key_exists('format', $configArray)) {
108
            $format = Init::initialize($configArray['format']);
109
        } else {
110
            $format = new IetfFormat();
111
        }
112
113
        return $format;
114
    }
115
}
116