Completed
Push — master ( 41dbe6...97c741 )
by Pavel
06:56 queued 12s
created

PHPConstantsOutputAdapter::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.072

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 10
cp 0.8
rs 9.4285
cc 3
eloc 7
nc 4
nop 4
crap 3.072
1
<?php
2
3
namespace Paro\EnvironmentParameters\Adapter\Output;
4
5
6
class PHPConstantsOutputAdapter implements OutputAdapterInterface
7
{
8
    public static function getName()
9
    {
10
        return 'php-constants';
11
    }
12
13 2
    public function process($parameters, $fileName, $env, $date = null)
14
    {
15 2
    	if (is_null($date)) {
16
    		$date = time();
17
	    }
18
19 2
        $content = sprintf("<?php\n/** This file is auto-generated during the build process of '%s' environment at %s **/\n", $env, date(DATE_ATOM, $date));
20 2
        foreach ($parameters as $key => $value) {
21 2
            $content .= sprintf("define('%s', %s);\n", $key, $this->serialize($value));
22 2
        };
23 2
        file_put_contents($fileName, $content);
24 2
    }
25
26
    /**
27
     * @param $value
28
     * @return string
29
     */
30 2
    protected function serialize($value)
31
    {
32 2
        switch (gettype($value)) {
33 2
            case 'boolean':
34
                return $value ? 'true' : 'false';
35 2
            case 'array':
36 2
            case 'object':
37 1
                return "'" . $this->addSlashes(serialize($value)) . "'";
38 1
            case 'NULL':
39
                return "null";
40 1
            case 'integer':
41 1
            case 'double':
42 1
                return $value;
43
            default:
44
                return "'" . $this->addSlashes($value) . "'";
45
        }
46
    }
47
48 1
    private function addSlashes($value)
49
    {
50 1
        return str_replace("'", "\\'", $value);
51
    }
52
53
}