Completed
Push — master ( 76d085...a562a5 )
by Pavel
01:58
created

FileHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 1
dl 0
loc 65
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolvePath() 0 9 2
A preparePath() 0 8 2
A getArgumentValue() 0 12 2
A initDirectory() 0 6 2
1
<?php
2
3
namespace Paro\BuildParametersHandler;
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