Passed
Pull Request — master (#32)
by
unknown
02:23
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\Hook;
11
12
use CaptainHook\App\Hooks;
13
use CaptainHook\App\Storage\Util as StorageUtil;
14
use RuntimeException;
15
16
/**
17
 * Class Util
18
 *
19
 * @package CaptainHook
20
 * @author  Sebastian Feldmann <[email protected]>
21
 * @link    https://github.com/captainhookphp/captainhook
22
 * @since   Class available since Release 0.9.0
23
 */
24
abstract class Util
25
{
26
    /**
27
     * Checks if a hook name is valid
28
     *
29
     * @param  string $hook
30
     * @return bool
31
     */
32 33
    public static function isValid(string $hook) : bool
33
    {
34 33
        return isset(Hooks::getValidHooks()[$hook]);
35
    }
36
37
    /**
38
     * Returns list of valid hooks
39
     *
40
     * @return array
41
     */
42 27
    public static function getValidHooks() : array
43
    {
44 27
        return Hooks::getValidHooks();
45
    }
46
47
    /**
48
     * Returns hooks command class
49
     *
50
     * @param  string $hook
51
     * @return string
52
     */
53 15
    public static function getHookCommand(string $hook) : string
54
    {
55 15
        if (!self::isValid($hook)) {
56 3
            throw new RuntimeException(sprintf('Hook \'%s\' is not supported', $hook));
57
        }
58 12
        return Hooks::getValidHooks()[$hook];
59
    }
60
61
    /**
62
     * Get a list of all supported hooks
63
     *
64
     * @return array
65
     */
66 4
    public static function getHooks() : array
67
    {
68 4
        return array_keys(Hooks::getValidHooks());
69
    }
70
71
    /**
72
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor.
73
     *
74
     * @param string $repoDir
75
     * @param string $targetPath
76
     *
77
     * @return string
78
     * @throws RuntimeException
79
     */
80 6
    public static function getTplTargetPath(string $repoDir, string $targetPath) : string
81
    {
82 6
        $repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
83 6
        $target = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR));
84
85 6
        if (!StorageUtil::isSubDirectoryOf($target, $repo)) {
86 3
            return '\'' . $targetPath;
87
        }
88
89 5
        return '__DIR__ . \'/../../' . StorageUtil::getSubPathOf($target, $repo);
90
    }
91
92 3
    public static function getBinaryPath(string $repoDir, string $vendorPath, string $binary): string
93
    {
94 3
        $repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
95 3
        $vendor = explode(DIRECTORY_SEPARATOR, ltrim($vendorPath, DIRECTORY_SEPARATOR));
96
97 3
        if (!StorageUtil::isSubDirectoryOf($vendor, $repo)) {
98 2
            return $vendorPath . '/bin/' . $binary;
99
        }
100
101 2
        return $binary;
102
    }
103
}
104