Completed
Pull Request — master (#32)
by
unknown
09:15
created

Util::getBinaryPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 6
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 35
     */
32
    public static function isValid(string $hook) : bool
33 35
    {
34
        return isset(Hooks::getValidHooks()[$hook]);
35
    }
36
37
    /**
38
     * Returns list of valid hooks
39
     *
40
     * @return array
41 29
     */
42
    public static function getValidHooks() : array
43 29
    {
44
        return Hooks::getValidHooks();
45
    }
46
47
    /**
48
     * Returns hooks command class
49
     *
50
     * @param  string $hook
51
     * @return string
52 17
     */
53
    public static function getHookCommand(string $hook) : string
54 17
    {
55 3
        if (!self::isValid($hook)) {
56
            throw new RuntimeException(sprintf('Hook \'%s\' is not supported', $hook));
57 14
        }
58
        return Hooks::getValidHooks()[$hook];
59
    }
60
61
    /**
62
     * Get a list of all supported hooks
63
     *
64
     * @return array
65 4
     */
66
    public static function getHooks() : array
67 4
    {
68
        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
    public static function getTplTargetPath(string $repoDir, string $targetPath) : string
81
    {
82
        $repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
83
        $target = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR));
84
85
        if (!StorageUtil::isSubDirectoryOf($target, $repo)) {
86
            return '\'' . $targetPath;
87
        }
88
89
        return '__DIR__ . \'/../../' . StorageUtil::getSubPathOf($target, $repo);
90
    }
91
92
    public static function getBinaryPath(string $repoDir, string $vendorPath, string $binary): string
93
    {
94
        $repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
95
        $vendor = explode(DIRECTORY_SEPARATOR, ltrim($vendorPath, DIRECTORY_SEPARATOR));
96
97
        if (!StorageUtil::isSubDirectoryOf($vendor, $repo)) {
98
            return $vendorPath . '/bin/' . $binary;
99
        }
100
101
        return $binary;
102
    }
103
}
104