Passed
Push — master ( a467a6...de53c5 )
by Sebastian
02:03
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
/**
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
declare(strict_types=1);
11
12
namespace CaptainHook\App\Hook\Template;
13
14
use CaptainHook\App\Hook\Template;
15
use CaptainHook\App\Storage\Util;
16
17
/**
18
 * Local class
19
 *
20
 * Generates the sourcecode for the php hook scripts in .git/hooks/*.
21
 *
22
 * @package CaptainHook
23
 * @author  Sebastian Feldmann <[email protected]>
24
 * @link    https://github.com/captainhookphp/captainhook
25
 * @since   Class available since Release 4.3.0
26
 */
27
class Local implements Template
28
{
29
    /**
30
     * Path to the vendor directory
31
     *
32
     * @var string
33
     */
34
    private $vendorPath;
35
36
    /**
37
     * Path to the captainhook configuration
38
     *
39
     * @var string
40
     */
41
    private $configPath;
42
43
    /**
44
     * Local constructor
45
     *
46
     * @param string $repoPath
47
     * @param string $vendorPath
48
     * @param string $configPath
49
     */
50 5
    public function __construct(string $repoPath, string $vendorPath, string $configPath)
51
    {
52 5
        $this->vendorPath = Util::getTplTargetPath($repoPath, $vendorPath);
53 5
        $this->configPath = Util::getTplTargetPath($repoPath, $configPath);
54 5
    }
55
56
    /**
57
     * Return the code for the git hook scripts
58
     *
59
     * @param  string $hook Name of the hook to generate the sourcecode for
60
     * @return string
61
     */
62 5
    public function getCode(string $hook): string
63
    {
64 5
        return '#!/usr/bin/env php' . PHP_EOL .
65 5
            '<?php' . PHP_EOL .
66 5
            '$autoLoader = ' . $this->vendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
67 5
            'if (!file_exists($autoLoader)) {' . PHP_EOL .
68 5
            '    fwrite(STDERR,' . PHP_EOL .
69 5
            '        \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL .
70 5
            '        \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL .
71 5
            '        \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL .
72 5
            '    );' . PHP_EOL .
73 5
            '    exit(1);' . PHP_EOL .
74 5
            '}' . PHP_EOL .
75 5
            'require $autoLoader;' . PHP_EOL .
76 5
            '$config = realpath(' . $this->configPath . '\');' . PHP_EOL .
77 5
            '$app    = new CaptainHook\App\Console\Application\Hook();' . PHP_EOL .
78 5
            '$app->setHook(\'' . $hook . '\');' . PHP_EOL .
79 5
            '$app->setConfigFile($config);' . PHP_EOL .
80 5
            '$app->run();' . PHP_EOL . PHP_EOL;
81
    }
82
}
83