ConfigurationValidator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 0 41 7
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-05-31 
5
 */
6
7
namespace Net\Bazzline\Component\Locator\Process\Validator;
8
9
use Net\Bazzline\Component\Locator\Configuration\Configuration;
10
use Net\Bazzline\Component\ProcessPipe\ExecutableException;
11
use Net\Bazzline\Component\ProcessPipe\ExecutableInterface;
12
13
class ConfigurationValidator implements ExecutableInterface
14
{
15
    /**
16
     * @param array $input
17
     * @return array
18
     * @throws ExecutableException
19
     */
20
    public function execute($input = null)
21
    {
22
        if (!is_array($input)) {
23
            throw new ExecutableException(
24
                'input must be an array'
25
            );
26
        }
27
28
        if (!($input['configuration'] instanceof Configuration)) {
29
            throw new ExecutableException(
30
                'input must an instance of Configuration'
31
            );
32
        }
33
        /** @var Configuration $configuration */
34
        $configuration  = $input['configuration'];
35
        $path           = $configuration->getFilePath();
36
37
        if (is_file($path)) {
38
            $message = 'provided path "' . $path . '" is an existing file';
39
40
            throw new ExecutableException($message);
41
        }
42
43
        if (!is_dir($path)) {
44
            $couldNotCreateNotExistingDirectory = !(mkdir($path));
45
46
            if ($couldNotCreateNotExistingDirectory) {
47
                $message = 'could not create directory "' . $path . '"';
48
49
                throw new ExecutableException($message);
50
            }
51
        }
52
53
        if (!is_writable($path)) {
54
            $message = 'provided directory "' . $path . '" is not writable';
55
56
            throw new ExecutableException($message);
57
        }
58
59
        return $input;
60
    }
61
}