Passed
Push — master ( 38ff33...644407 )
by Sebastian
01:50
created

Builder::getRelativePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Config;
15
use CaptainHook\App\Console\IOUtil;
16
use CaptainHook\App\Hook\Template;
17
use Captainhook\App\Storage\Util;
18
use SebastianFeldmann\Git\Repository;
19
use Symfony\Component\Console\Input\InputInterface;
20
21
/**
22
 * Builder class
23
 *
24
 * Creates git hook Template objects regarding some provided input.
25
 *
26
 * @package CaptainHook
27
 * @author  Sebastian Feldmann <[email protected]>
28
 * @link    https://github.com/captainhookphp/captainhook
29
 * @since   Class available since Release 4.3.0
30
 */
31
abstract class Builder
32
{
33
    /**
34
     * Creates a template that is responsible for the git hook template
35
     *
36
     * @param \Symfony\Component\Console\Input\InputInterface $input
37
     * @param \CaptainHook\App\Config                         $config
38
     * @param \SebastianFeldmann\Git\Repository               $repository
39
     *
40
     * @return \CaptainHook\App\Hook\Template
41
     */
42 5
    public static function build(InputInterface $input, Config $config, Repository $repository): Template
43
    {
44 5
        if ($input->getOption('run-mode') === Template::DOCKER) {
45
            // For docker we need to strip down the current working directory.
46
            // This is caused because docker will always connect to a specific working directory
47
            // where the absolute path will not be recognized.
48
            // E.g.:
49
            //   cwd => /docker
50
            //   path => /docker/captainhook-run
51
            // The actual path needs to be /captainhook-run to work
52 1
            $repoPath = self::getRelativePath((string) realpath($repository->getRoot()));
53
54 1
            return new Docker(
55 1
                $repoPath,
56 1
                'vendor',
57 1
                IOUtil::argToString($input->getOption('container'))
58
            );
59
        }
60
61 4
        return new Local(
62 4
            (string) realpath($repository->getRoot()),
63 4
            getcwd() . '/vendor',
64 4
            (string) realpath($config->getPath())
65
        );
66
    }
67
68
    /**
69
     * Transforms an absolute path to a relative one
70
     *
71
     * @param string $path
72
     * @return string
73
     */
74 1
    private static function getRelativePath(string $path)
75
    {
76 1
        return Util::getSubPathOf(
77 1
            Util::pathToArray($path),
78 1
            Util::pathToArray(getcwd())
79
        );
80
    }
81
}
82