Completed
Pull Request — master (#32)
by
unknown
08:40
created

Runner   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 27
dl 0
loc 56
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCode() 0 24 1
A getTplTargetPath() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace CaptainHook\App\Hook;
5
6
use CaptainHook\App\Storage\Util as StorageUtil;
7
8
abstract class Runner
9
{
10
    /**
11
     * Return the php code for the git hook runner.
12
     *
13
     * @param string $repoPath   Absolute path to the repository root.
14
     * @param string $vendorPath Absolute path to the vendor folder.
15
     * @param string $configPath Absolute path to the configuration file.
16
     *
17
     * @return string
18
     */
19
    public static function getCode(string $repoPath, string $vendorPath, string $configPath): string
20
    {
21
        $tplVendorPath = self::getTplTargetPath($repoPath, $vendorPath);
22
        $tplConfigPath = self::getTplTargetPath($repoPath, $configPath);
23
24
        return '#!/usr/bin/env php' . PHP_EOL .
25
            '<?php' . PHP_EOL .
26
            '$autoLoader = ' . $tplVendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
27
            'if (!file_exists($autoLoader)) {' . PHP_EOL .
28
            '    fwrite(STDERR,' . PHP_EOL .
29
            '        \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL .
30
            '        \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL .
31
            '        \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL .
32
            '    );' . PHP_EOL .
33
            '    exit(1);' . PHP_EOL .
34
            '}' . PHP_EOL .
35
            '$hook = $_SERVER[\'argv\'][1];' . PHP_EOL .
36
            'array_splice($_SERVER[\'argv\'], 1, 1);' . PHP_EOL .
37
            'require $autoLoader;' . PHP_EOL .
38
            '$config = realpath(' . $tplConfigPath . '\');' . PHP_EOL .
39
            '$app    = new CaptainHook\App\Console\Application\Hook();' . PHP_EOL .
40
            '$app->setHook($hook);' . PHP_EOL .
41
            '$app->setConfigFile($config);' . PHP_EOL .
42
            '$app->run();' . PHP_EOL . PHP_EOL;
43
    }
44
45
    /**
46
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor.
47
     *
48
     * @param string $repoDir
49
     * @param string $targetPath
50
     *
51
     * @return string
52
     * @throws \RuntimeException
53
     */
54
    public static function getTplTargetPath(string $repoDir, string $targetPath): string
55
    {
56
        $repo = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
57
        $target = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR));
58
59
        if (!StorageUtil::isSubDirectoryOf($target, $repo)) {
60
            return '\'' . $targetPath;
61
        }
62
63
        return '__DIR__ . \'/../../' . StorageUtil::getSubPathOf($target, $repo);
64
    }
65
}