Completed
Branch master (a08ded)
by Pavel
01:58
created

FileHandler::findFile()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 24
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 5
nop 1
crap 56
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
    public function __construct(Filesystem $fs, array $arguments)
24
    {
25
        $this->fs = $fs;
26
        $this->arguments = $arguments;
27
    }
28
29
    public function resolvePath($currentPath, $importPath)
30
    {
31
        if ($this->fs->isAbsolutePath($importPath)) {
32
            return $this->preparePath($importPath);
33
        } else {
34
            $path = dirname($currentPath) . DIRECTORY_SEPARATOR . $importPath;
35
            if (substr($path, 0, 4) == '././') {
36
                $path = substr($path, 2);
37
            }
38
            return $this->preparePath($path);
39
        }
40
    }
41
42
    /**
43
     * @param $path
44
     * @param null $env
45
     * @return mixed
46
     */
47
    public function preparePath($path, $env = null)
48
    {
49
        if (is_null($env)) {
50
            $env = $this->getArgumentValue('env');
51
        }
52
53
        if ($env !== false) {
54
            return str_replace('{env}', $env, $path);
55
        } else {
56
            return $path;
57
        }
58
    }
59
60
    /**
61
     * @param $name
62
     * @return string|false
63
     */
64
    public function getArgumentValue($name)
65
    {
66
        return array_reduce($this->arguments, function($carry, $item) use ($name) {
67
            if (substr(strtolower($item), 0, strlen($name) + 2) == '--' . $name) {
68
                $val = explode('=', $item);
69
                return trim($val[1]);
70
            } else {
71
                return $carry;
72
            }
73
        }, false);
74
75
    }
76
77
    public function initDirectory($dir)
78
    {
79
        if (!$this->fs->exists($dir)) {
80
            $this->fs->mkdir($dir);
81
        }
82
    }
83
84
    public function findFile($path)
85
    {
86
        $env = $this->getArgumentValue('env');
87
        if ($env !== false && strpos($env, DIRECTORY_SEPARATOR) > 0) {
88
            $envParts = explode(DIRECTORY_SEPARATOR, $env);
89
            while (count($envParts) > 0) {
90
                $fileName = $this->preparePath($path, join(DIRECTORY_SEPARATOR, $envParts));
91
                if ($this->fs->exists($fileName)) {
92
                    return $fileName;
93
                }
94
                unset($envParts[count($envParts) - 1]);
95
            }
96
            //root folder
97
            $fileName = str_replace('{env}/', '', $path);
98
            if ($this->fs->exists($fileName)) {
99
                return $fileName;
100
            }
101
        } else {
102
            $fileName = $this->preparePath($path);
103
            if ($this->fs->exists($fileName)) {
104
                return $fileName;
105
            }
106
        }
107
108
        throw new \InvalidArgumentException(sprintf('File "%s" for environment "%s" doesn\'t exists', $path, $env));
109
    }
110
111
    /**
112
     * @param $itemSource
113
     * @return string
114
     */
115
    public function processEnvironmentalVariable($itemSource)
116
    {
117
        $item = trim($itemSource);
118
        if (substr(strtolower($item), 0, 5) === "%env(" && substr(strtolower($item), -2) == ')%') {
119
            $envName = substr(trim($item), 5);
120
            $envName = substr($envName, 0, strlen($envName) - 2);
121
            if (($envValue = getenv($envName)) === false) {
122
                throw new \InvalidArgumentException(sprintf('Environment variable "%s" doesn\'t exist', $envName));
123
            } else {
124
                return $envValue;
125
            }
126
        } else {
127
            return $itemSource;
128
        }
129
130
    }
131
}
132