Passed
Pull Request — master (#32)
by
unknown
02:18
created

Local::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 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 5
    public function __construct(string $repoPath, string $vendorPath, string $configPath)
21
    {
22 5
        $this->vendorPath = HookUtil::getTplTargetPath($repoPath, $vendorPath);
23 5
        $this->configPath = HookUtil::getTplTargetPath($repoPath, $configPath);
24 5
    }
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 5
    public function getCode(string $hook): string
34
    {
35 5
        return '#!/usr/bin/env php' . PHP_EOL .
36 5
            '<?php' . PHP_EOL .
37 5
            '$autoLoader = ' . $this->vendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
38 5
            'if (!file_exists($autoLoader)) {' . PHP_EOL .
39 5
            '    fwrite(STDERR,' . PHP_EOL .
40 5
            '        \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL .
41 5
            '        \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL .
42 5
            '        \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL .
43 5
            '    );' . PHP_EOL .
44 5
            '    exit(1);' . PHP_EOL .
45 5
            '}' . PHP_EOL .
46 5
            'require $autoLoader;' . PHP_EOL .
47 5
            '$config = realpath(' . $this->configPath . '\');' . PHP_EOL .
48 5
            '$app    = new CaptainHook\App\Console\Application\Hook();' . PHP_EOL .
49 5
            '$app->setHook(\'' . $hook . '\');' . PHP_EOL .
50 5
            '$app->setConfigFile($config);' . PHP_EOL .
51 5
            '$app->run();' . PHP_EOL . PHP_EOL;
52
    }
53
}