Passed
Push — main ( 388f81...59708d )
by Sebastian
03:37
created

PathInfo::getExecutablePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Template;
13
14
use SebastianFeldmann\Camino\Check;
15
use SebastianFeldmann\Camino\Path;
16
use SebastianFeldmann\Camino\Path\Directory;
17
use SebastianFeldmann\Camino\Path\File;
18
19
class PathInfo
20
{
21
    /**
22
     * Absolute path to repository
23
     * @var string
24
     */
25
    private string $repositoryAbsolute;
26
27
    /**
28
     * @var \SebastianFeldmann\Camino\Path\Directory
29
     */
30
    protected Directory $repository;
31
32
    /**
33
     * Absolute path to config file
34
     * @var string
35
     */
36
    private string $configAbsolute;
37
38
    /**
39
     * @var \SebastianFeldmann\Camino\Path\File
40
     */
41
    protected File $config;
42
43
    /**
44
     * Absolute path to captainhook executable
45
     * @var string
46
     */
47
    protected string $executableAbsolute;
48
49
    /**
50
     * @var \SebastianFeldmann\Camino\Path\File
51
     */
52
    private File $executable;
53
54
    /**
55
     * PHAR or composer runtime
56
     *
57
     * @var bool
58
     */
59
    private bool $isPhar;
60
61
    /**
62
     * @param string $repositoryPath
63
     * @param string $configPath
64
     * @param string $execPath
65
     * @param bool   $isPhar
66
     */
67 21
    public function __construct(string $repositoryPath, string $configPath, string $execPath, bool $isPhar)
68
    {
69 21
        $this->repositoryAbsolute = self::toAbsolutePath($repositoryPath);
70 21
        $this->repository         = new Directory($this->repositoryAbsolute);
71 21
        $this->configAbsolute     = self::toAbsolutePath($configPath);
72 21
        $this->config             = new File($this->configAbsolute);
73 21
        $this->executableAbsolute = self::toAbsolutePath($execPath);
74 21
        $this->executable         = new File($this->executableAbsolute);
75 20
        $this->isPhar             = $isPhar;
76
    }
77
78
    /**
79
     * Returns the path to the captainhook executable
80
     *
81
     * @return string
82
     */
83 14
    public function getExecutablePath(): string
84
    {
85
        // check if the captainhook binary is in the repository bin directory
86
        // this should only be the case if we work in the captainhook repository
87 14
        if (file_exists($this->repositoryAbsolute . '/bin/captainhook')) {
88 3
            return './bin/captainhook';
89
        }
90 11
        return $this->getPathFromTo($this->repository, $this->executable);
91
    }
92
93
    /**
94
     * Returns the path to the captainhook configuration file
95
     * @return string
96
     */
97 16
    public function getConfigPath(): string
98
    {
99 16
        return $this->getPathFromTo($this->repository, $this->config);
100
    }
101
102
    /**
103
     * Runtime indicator
104
     *
105
     * @return bool
106
     */
107 16
    public function isPhar(): bool
108
    {
109 16
        return $this->isPhar;
110
    }
111
112
    /**
113
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor
114
     *
115
     * @param  \SebastianFeldmann\Camino\Path\Directory $repo
116
     * @param  \SebastianFeldmann\Camino\Path           $target
117
     * @return string
118
     */
119 16
    private function getPathFromTo(Directory $repo, Path $target): string
120
    {
121 16
        if (!$target->isChildOf($repo)) {
122 9
            return $target->getPath();
123
        }
124 8
        return $target->getRelativePathFrom($repo);
125
    }
126
127
128
    /**
129
     * Make sure the given path is absolute
130
     *
131
     * @param  string $path
132
     * @return string
133
     */
134 21
    private static function toAbsolutePath(string $path): string
135
    {
136 21
        if (Check::isAbsolutePath($path)) {
137 21
            return $path;
138
        }
139 1
        return (string) realpath($path);
140
    }
141
}
142