Completed
Push — master ( b25810...38231d )
by Pavel
01:56
created

FileHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Paro\BuildParametersHandler;
4
5
class FileHandler
6
{
7
    /**
8
     * @var array
9
     */
10
    private $arguments;
11
12
    /**
13
     * FileHandler constructor.
14
     * @param array $arguments
15
     */
16 3
    public function __construct(array $arguments)
17
    {
18 3
        $this->arguments = $arguments;
19 3
    }
20
21
    public function resolvePath($currentPath, $importPath) {
22
        if (substr($importPath, 0, 1) == '/') {
23
            return $this->preparePath($importPath);
24
        } else {
25
            $path = dirname($currentPath) . DIRECTORY_SEPARATOR . $importPath;
26
            return $this->preparePath($path);
27
        }
28
    }
29
30 2
    public function preparePath($path) {
31 2
        if (($env = $this->getEnvParameter('env')) !== false) {
32 2
            return str_replace("{env}", $env, $path);
33
        } else {
34
            return $path;
35
        }
36
    }
37
38
    public function getEnvParameter($name) {
39 2
        return array_reduce($this->arguments, function($ret, $item) use ($name) {
40 2
            if (substr(strtolower($item), 0, 5) == '--' . $name) {
41 2
                $val = explode('=', $item);
42 2
                return trim($val[1]);
43
            }
44 2
        }, false);
45
46
    }
47
48
    public function initDirectory($dir) {
49
        if (!is_dir($dir)) {
50
            mkdir($dir);
51
        }
52
    }
53
}
54