Passed
Push — master ( 457c92...a5b7b1 )
by Sebastian
02:34
created

Local::getPathFromHookTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 2
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
declare(strict_types=1);
11
12
namespace CaptainHook\App\Hook\Template;
13
14
use CaptainHook\App\Hook\Template;
15
use CaptainHook\App\Storage\Util;
0 ignored issues
show
Bug introduced by
The type CaptainHook\App\Storage\Util was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SebastianFeldmann\Camino\Path;
17
use SebastianFeldmann\Camino\Path\Directory;
18
use SebastianFeldmann\Camino\Path\File;
19
20
/**
21
 * Local class
22
 *
23
 * Generates the sourcecode for the php hook scripts in .git/hooks/*.
24
 *
25
 * @package CaptainHook
26
 * @author  Sebastian Feldmann <[email protected]>
27
 * @link    https://github.com/captainhookphp/captainhook
28
 * @since   Class available since Release 4.3.0
29
 */
30
class Local implements Template
31
{
32
    /**
33
     * Path to the vendor directory
34
     *
35
     * @var string
36
     */
37
    private $vendorPath;
38
39
    /**
40
     * Path to the captainhook configuration
41
     *
42
     * @var string
43
     */
44
    private $configPath;
45
46
    /**
47
     * Local constructor
48
     *
49
     * @param \SebastianFeldmann\Camino\Path\Directory $repo
50 5
     * @param \SebastianFeldmann\Camino\Path\Directory $vendor
51
     * @param \SebastianFeldmann\Camino\Path\File      $config
52 5
     */
53 5
    public function __construct(Directory $repo, Directory $vendor, File $config)
54 5
    {
55
        $this->vendorPath = $this->getPathFromHookTo($repo, $vendor);
56
        $this->configPath = $this->getPathFromHookTo($repo, $config);
57
    }
58
59
    /**
60
     * Return the code for the git hook scripts
61
     *
62 5
     * @param  string $hook Name of the hook to generate the sourcecode for
63
     * @return string
64 5
     */
65 5
    public function getCode(string $hook): string
66 5
    {
67 5
        return '#!/usr/bin/env php' . PHP_EOL .
68 5
            '<?php' . PHP_EOL .
69 5
            '$autoLoader = ' . $this->vendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
70 5
            'if (!file_exists($autoLoader)) {' . PHP_EOL .
71 5
            '    fwrite(STDERR, \'Composer autoload.php could not be found\');' . PHP_EOL .
72 5
            '    exit(1);' . PHP_EOL .
73 5
            '}' . PHP_EOL .
74 5
            'require $autoLoader;' . PHP_EOL .
75 5
            '$config = realpath(' . $this->configPath . '\');' . PHP_EOL .
76 5
            '$app    = new CaptainHook\App\Console\Application\Hook();' . PHP_EOL .
77 5
            '$app->setHook(\'' . $hook . '\');' . PHP_EOL .
78
            '$app->setConfigFile($config);' . PHP_EOL .
79
            '$app->setRepositoryPath(dirname(dirname(__DIR__)));' . PHP_EOL .
80
            '$app->run();' . PHP_EOL . PHP_EOL;
81
    }
82
83
    /**
84
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor
85
     *
86
     * @param  \SebastianFeldmann\Camino\Path\Directory $repo
87
     * @param  \SebastianFeldmann\Camino\Path           $target
88
     * @return string
89
     */
90
    private function getPathFromHookTo(Directory $repo, Path $target) : string
91
    {
92
        if (!$target->isChildOf($repo)) {
93
            return $target->getPath();
94
        }
95
96
        return '__DIR__ . \'/../../' . $target->getRelativePathFrom($repo);
97
    }
98
}
99