Completed
Push — master ( 1081d6...4c9b6d )
by Pavel
03:03
created

FileHandler::resolvePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Paro\EnvironmentParameters;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
7
class FileHandler
8
{
9
    /**
10
     * @var array
11
     */
12
    private $arguments;
13
    /**
14
     * @var Filesystem
15
     */
16
    private $fs;
17
18
    /**
19
     * FileHandler constructor.
20
     * @param Filesystem $fs
21
     * @param array $arguments
22
     */
23 11
    public function __construct(Filesystem $fs, array $arguments)
24
    {
25 11
        $this->fs = $fs;
26 10
        $this->arguments = $arguments;
27 10
    }
28
29 4
    public function resolvePath($currentPath, $importPath)
30
    {
31 4
        if (substr($importPath, 0, 1) == DIRECTORY_SEPARATOR) {
32 1
            return $this->preparePath($importPath);
33
        } else {
34 3
            $path = dirname($currentPath) . DIRECTORY_SEPARATOR . $importPath;
35 3
            return $this->preparePath($path);
36
        }
37
    }
38
39 9
    public function preparePath($path)
40
    {
41 9
        if (($env = $this->getArgumentValue('env')) !== false) {
42 9
            return str_replace("{env}", $env, $path);
43
        } else {
44
            return $path;
45
        }
46
    }
47
48
    /**
49
     * @param $name
50
     * @return string|false
51
     */
52
    public function getArgumentValue($name)
53
    {
54 9
        return array_reduce($this->arguments, function($carry, $item) use ($name) {
55 9
            if (substr(strtolower($item), 0, strlen($name) + 2) == '--' . $name) {
56 8
                $val = explode('=', $item);
57 8
                return trim($val[1]);
58
            } else {
59 1
                return $carry;
60
            }
61 9
        }, false);
62
63
    }
64
65 1
    public function initDirectory($dir)
66
    {
67 1
        if (!$this->fs->exists($dir)) {
68 1
            $this->fs->mkdir($dir);
69 1
        }
70 1
    }
71
}
72