FileSystem::checkDirectory()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php namespace Tequilarapido\Cli;
2
3
use Symfony\Component\Process\Process;
4
5
class FileSystem
6
{
7
8
    public function checkDirectory($dir)
9
    {
10
        // Check if it's not empty
11
        if (empty($dir)) {
12
            throw new \LogicException("Empty given directory.");
13
        }
14
15
        // Check if it exists and it is writable
16
        $directory = $this->getDirFullPath($dir);
17
        if (!$directory) {
18
            throw new \LogicException("Directory not found (Given:" . $dir . ").");
19
        }
20
21
        return $directory;
22
    }
23
24
25
    protected function getDirFullPath($dir)
26
    {
27
        // Let's try with nothing : full path ?
28
        if (is_dir($dir)) {
29
            return $dir;
30
        }
31
32
        // Let's try current directory
33
        $cd = getcwd();
34
        $fullpath = $cd . '/' . $dir;
35
        if ($cd && is_dir($fullpath)) {
36
            return $fullpath;
37
        }
38
39
        // If we are on windows using Cygwin, tryout Windows path
40
        $cmd = "cygpath -w $dir";
41
        $process = new Process($cmd);
42
        $process->run();
43
        if ($process->isSuccessful()) {
44
            return trim($process->getOutput());
45
        }
46
47
        return false;
48
    }
49
50
}