Passed
Push — master ( a467a6...de53c5 )
by Sebastian
02:03
created

Util::getTplTargetPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace CaptainHook\App\Storage;
11
12
/**
13
 * Util class
14
 *
15
 * @package CaptainHook
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/captainhookphp/captainhook
18
 * @since   Class available since Release 1.0.4
19
 */
20
class Util
21
{
22
    /**
23
     * Array representation of a path.
24
     *
25
     * @param  string $path
26
     * @return array
27
     */
28 4
    public static function pathToArray(string $path) : array
29
    {
30 4
        return explode(DIRECTORY_SEPARATOR, ltrim($path, DIRECTORY_SEPARATOR));
31
    }
32
33
    /**
34
     * Is the given subDir a sub directory of given parentDir.
35
     *
36
     * @param  array $subDir
37
     * @param  array $parentDir
38
     * @return bool
39
     */
40 11
    public static function isSubDirectoryOf(array $subDir, array $parentDir) : bool
41
    {
42 11
        foreach ($parentDir as $index => $dir) {
43 11
            if (!isset($subDir[$index]) || $dir !== $subDir[$index]) {
44 11
                return false;
45
            }
46
        }
47 8
        return true;
48
    }
49
50
    /**
51
     * Return the relative path from parentDir to subDir.
52
     *
53
     * @param  array $subDir
54
     * @param  array $parentDir
55
     * @return string
56
     */
57 6
    public static function getSubPathOf(array $subDir, array $parentDir) : string
58
    {
59 6
        if (!self::isSubDirectoryOf($subDir, $parentDir)) {
60 1
            throw new \RuntimeException('Invalid sub directory');
61
        }
62
63 5
        $path = [];
64 5
        foreach (array_slice($subDir, count($parentDir)) as $dir) {
65 5
            $path[] = $dir;
66
        }
67 5
        return implode(DIRECTORY_SEPARATOR, $path);
68
    }
69
70
    /**
71
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor
72
     *
73
     * @param  string $repoDir
74
     * @param  string $targetPath
75
     * @return string
76
     * @throws \RuntimeException
77
     */
78 6
    public static function getTplTargetPath(string $repoDir, string $targetPath) : string
79
    {
80 6
        $repo   = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
81 6
        $target = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR));
82
83 6
        if (!self::isSubDirectoryOf($target, $repo)) {
84 3
            return '\'' . $targetPath;
85
        }
86
87 5
        return '__DIR__ . \'/../../' . self::getSubPathOf($target, $repo);
88
    }
89
90
    /**
91
     * Returns the path to the captainhook-run binary
92
     *
93
     * @param  string $repoDir
94
     * @param  string $vendorPath
95
     * @param  string $binary
96
     * @return string
97
     */
98 3
    public static function getBinaryPath(string $repoDir, string $vendorPath, string $binary): string
99
    {
100 3
        $repo   = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
101 3
        $vendor = explode(DIRECTORY_SEPARATOR, ltrim($vendorPath, DIRECTORY_SEPARATOR));
102
103 3
        if (!self::isSubDirectoryOf($vendor, $repo)) {
104 2
            return $vendorPath . '/bin/' . $binary;
105
        }
106
107 2
        return $binary;
108
    }
109
}
110