Completed
Push — master ( cb7793...7b1e0a )
by Arne
01:36
created

ConfigurationFileReader::getConfiguration()   D

Complexity

Conditions 17
Paths 34

Size

Total Lines 81
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 81
rs 4.984
c 0
b 0
f 0
cc 17
eloc 37
nc 34
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Archivr;
4
5
use Archivr\Exception\ConfigurationException;
6
use Archivr\Exception\Exception;
7
use Zend\Stdlib\ArrayUtils;
8
9
class ConfigurationFileReader
10
{
11
    public function getConfiguration(string $configurationFilePath)
12
    {
13
        if (!is_file($configurationFilePath) || !is_readable($configurationFilePath))
14
        {
15
            throw new Exception(sprintf('Configuration file path "%s" does not exist or is not readable.', $configurationFilePath));
16
        }
17
18
        $configurationFilePath = realpath($configurationFilePath);
19
20
        $json = file_get_contents($configurationFilePath);
21
22
        if (!$json)
23
        {
24
            throw new Exception(sprintf('Failed to read config file "%s".', $configurationFilePath));
25
        }
26
27
        $array = json_decode($json, true);
28
29
        if ($array === null)
30
        {
31
            throw new Exception(sprintf('Invalid configuration file: %s.', $configurationFilePath));
32
        }
33
34
        $array = ArrayUtils::merge([
35
            'path' => dirname($configurationFilePath)
36
        ], $array);
37
38
        foreach (['path', 'vaults'] as $requiredKey)
39
        {
40
            if (!array_key_exists($requiredKey, $array))
41
            {
42
                throw new ConfigurationException(sprintf('Missing config key: %s.', $requiredKey));
43
            }
44
        }
45
46
        if (!is_array($array['vaults']))
47
        {
48
            throw new ConfigurationException(sprintf('Configuration key \'vaults\' has to be an array.'));
49
        }
50
51
        if (empty($array['vaults']))
52
        {
53
            throw new ConfigurationException(sprintf('At least one vault configuration has to be present.'));
54
        }
55
56
        $configuration = new Configuration();
57
        $configuration->setLocalPath($array['path']);
58
        $configuration->setIdentity(empty($array['identity']) ? sprintf('%s@%s', get_current_user(), gethostname()) : $array['identity']);
0 ignored issues
show
Bug introduced by
It seems like empty($array['identity']...)) : $array['identity'] can also be of type array; however, Archivr\Configuration::setIdentity() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
60
        if (!empty($array['exclude']))
61
        {
62
            if (!is_array($array['exclude']))
63
            {
64
                throw new ConfigurationException('Config key "exclude" has to be an array.');
65
            }
66
67
            $configuration->setExclusions($array['exclude']);
68
        }
69
70
        foreach ($array['vaults'] as $index => $vaultConfig)
71
        {
72
            if (empty($vaultConfig['adapter']))
73
            {
74
                throw new ConfigurationException(sprintf('Vault configuration #%d is missing the obligatory \'adapter\' key.', $index));
75
            }
76
77
            $lockAdapter = empty($vaultConfig['lockAdapter']) ? 'connection' : $vaultConfig['lockAdapter'];
78
79
            $connectionConfig = new ConnectionConfiguration($vaultConfig['adapter'], $lockAdapter);
80
            $connectionConfig->setSettings($vaultConfig['settings'] ?: []);
81
82
            if (!empty($vaultConfig['title']))
83
            {
84
                $connectionConfig->setTitle($vaultConfig['title']);
85
            }
86
87
            $configuration->addConnectionConfiguration($connectionConfig);
88
        }
89
90
        return $configuration;
91
    }
92
}
93