DumpConfigurationContent::dumpConfiguration()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 4
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-07-11 
5
 */
6
7
namespace NetBazzlineZfCliGenerator\Service\ProcessPipe\Transformer;
8
9
use Net\Bazzline\Component\ProcessPipe\ExecutableException;
10
use Net\Bazzline\Component\ProcessPipe\ExecutableInterface;
11
12
class DumpConfigurationContent implements ExecutableInterface
13
{
14
    /** @var int */
15
    private $timestamp;
16
17
    /**
18
     * @param int $timestamp
19
     */
20
    public function setTimestamp($timestamp)
21
    {
22
        $this->timestamp = $timestamp;
23
    }
24
25
    /**
26
     * @param mixed $input
27
     * @return mixed
28
     * @throws ExecutableException
29
     */
30
    public function execute($input = null)
31
    {
32
        if (!is_array($input)) {
33
            throw new ExecutableException(
34
                'input must be an array'
35
            );
36
        }
37
38
        if (empty($input)) {
39
            throw new ExecutableException(
40
                'empty input provided'
41
            );
42
        }
43
44
        $output = '<?php' . PHP_EOL;
45
        $output .= '/**' . PHP_EOL .
46
            ' * @author Net\Bazzline Zf Cli Generator' . PHP_EOL .
47
            ' * @since ' . date('Y-m-d H:i:s', $this->timestamp) . PHP_EOL .
48
            ' */' . PHP_EOL . PHP_EOL;
49
        $output .= 'return array(' . PHP_EOL;
50
        $output .= $this->dumpConfiguration($input, 'callApplication');
51
        $output .= ');';
52
53
        return $output;
54
    }
55
56
    /**
57
     * @param array $configuration
58
     * @param string $closureName
59
     * @param string $indention
60
     * @param int $level
61
     * @return string
62
     * @todo make closureName configurable
63
     */
64
    private function dumpConfiguration(array $configuration, $closureName, $indention = '    ', $level = 1)
65
    {
66
        $content        = '';
67
        $localIndention = str_repeat($indention, $level);
68
        end($configuration);
69
        $lastIndex      = key($configuration);
70
71
        foreach ($configuration as $index => $values) {
72
            if (empty($values)) {
73
                $content .= $localIndention . '\'' . $index . '\' => $' . $closureName;
74
            } else {
75
                $content .= $localIndention . '\'' . $index . '\' => array(' . PHP_EOL;
76
                $content .= $this->dumpConfiguration($values, $closureName, $indention, $level + 1);
77
                $content .= $localIndention . ')';
78
            }
79
            if ($index !== $lastIndex) {
80
                $content .= ',';
81
            }
82
            $content .= PHP_EOL;
83
        }
84
85
        return $content;
86
    }
87
}