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

Local   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 22
dl 0
loc 43
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCode() 0 19 1
1
<?php
2
declare(strict_types=1);
3
4
namespace CaptainHook\App\Hook\Template;
5
6
use CaptainHook\App\Hook\Template;
7
use CaptainHook\App\Hook\Util as HookUtil;
8
9
class Local implements Template
10
{
11
    /**
12
     * @var string
13
     */
14
    private $vendorPath;
15
    /**
16
     * @var string
17
     */
18
    private $configPath;
19
20
    public function __construct(string $repoPath, string $vendorPath, string $configPath)
21
    {
22
        $this->vendorPath = HookUtil::getTplTargetPath($repoPath, $vendorPath);
23
        $this->configPath = HookUtil::getTplTargetPath($repoPath, $configPath);
24
    }
25
26
    /**
27
     * Return the code for the git hook scripts.
28
     *
29
     * @param string $hook Name of the hook to trigger.
30
     *
31
     * @return string
32
     */
33
    public function getCode(string $hook): string
34
    {
35
        return '#!/usr/bin/env php' . PHP_EOL .
36
            '<?php' . PHP_EOL .
37
            '$autoLoader = ' . $this->vendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
38
            'if (!file_exists($autoLoader)) {' . PHP_EOL .
39
            '    fwrite(STDERR,' . PHP_EOL .
40
            '        \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL .
41
            '        \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL .
42
            '        \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL .
43
            '    );' . PHP_EOL .
44
            '    exit(1);' . PHP_EOL .
45
            '}' . PHP_EOL .
46
            'require $autoLoader;' . PHP_EOL .
47
            '$config = realpath(' . $this->configPath . '\');' . PHP_EOL .
48
            '$app    = new CaptainHook\App\Console\Application\Hook();' . PHP_EOL .
49
            '$app->setHook(\'' . $hook . '\');' . PHP_EOL .
50
            '$app->setConfigFile($config);' . PHP_EOL .
51
            '$app->run();' . PHP_EOL . PHP_EOL;
52
    }
53
}