Completed
Push — master ( 4c9b6d...f5e1e2 )
by Pavel
02:13
created

FileHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 68
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A resolvePath() 0 12 3
A preparePath() 0 8 2
A getArgumentValue() 0 12 2
A initDirectory() 0 6 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 ($this->fs->isAbsolutePath($importPath)) {
32 1
            return $this->preparePath($importPath);
33
        } else {
34 3
            $path = dirname($currentPath) . DIRECTORY_SEPARATOR . $importPath;
35 3
            if (substr($path, 0, 4) == '././') {
36
                $path = substr($path, 2);
37
            }
38 3
            return $this->preparePath($path);
39
        }
40
    }
41
42 9
    public function preparePath($path)
43
    {
44 9
        if (($env = $this->getArgumentValue('env')) !== false) {
45 9
            return str_replace("{env}", $env, $path);
46
        } else {
47
            return $path;
48
        }
49
    }
50
51
    /**
52
     * @param $name
53
     * @return string|false
54
     */
55
    public function getArgumentValue($name)
56
    {
57 9
        return array_reduce($this->arguments, function($carry, $item) use ($name) {
58 9
            if (substr(strtolower($item), 0, strlen($name) + 2) == '--' . $name) {
59 8
                $val = explode('=', $item);
60 8
                return trim($val[1]);
61
            } else {
62 1
                return $carry;
63
            }
64 9
        }, false);
65
66
    }
67
68 1
    public function initDirectory($dir)
69
    {
70 1
        if (!$this->fs->exists($dir)) {
71 1
            $this->fs->mkdir($dir);
72 1
        }
73 1
    }
74
}
75