Completed
Push — master ( 7b4301...3d29af )
by Sebastian
05:32
created

Builder::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 31
ccs 13
cts 14
cp 0.9286
rs 9.7666
cc 3
nc 3
nop 2
crap 3.0032
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\Hook\Template;
16
use CaptainHook\App\Storage\Util;
17
use RuntimeException;
18
use SebastianFeldmann\Git\Repository;
19
20
/**
21
 * Builder class
22
 *
23
 * Creates git hook Template objects regarding some provided input.
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
abstract class Builder
31
{
32
    /**
33
     * Creates a template that is responsible for the git hook template
34
     *
35
     * @param  \CaptainHook\App\Config                         $config
36
     * @param  \SebastianFeldmann\Git\Repository               $repository
37
     * @return \CaptainHook\App\Hook\Template
38
     */
39 6
    public static function build(Config $config, Repository $repository): Template
40
    {
41 6
        $vendorPath     = (string) realpath($config->getVendorDirectory());
42 6
        $repositoryPath = (string) realpath($repository->getRoot());
43 6
        $configPath     = (string) realpath($config->getPath());
44
45 6
        if (empty($vendorPath)) {
46
            throw new RuntimeException('composer vendor directory not found');
47
        }
48
49 6
        if ($config->getRunMode() === Template::DOCKER) {
50
            // For docker we need to strip down the current working directory.
51
            // This is caused because docker will always connect to a specific working directory
52
            // where the absolute path will not be recognized.
53
            // E.g.:
54
            //   cwd => /docker
55
            //   path => /docker/captainhook-run
56
            // The actual path needs to be /captainhook-run to work
57 2
            $dockerRepoPath = self::getRelativePath($repositoryPath);
58
59 2
            return new Docker(
60 2
                $dockerRepoPath,
61 2
                $config->getVendorDirectory(),
62 2
                $config->getRunExec()
63
            );
64
        }
65
66 4
        return new Local(
67 4
            $repositoryPath,
68
            $vendorPath,
69
            $configPath
70
        );
71
    }
72
73
    /**
74
     * Transforms an absolute path to a relative one
75
     *
76
     * @param  string $path
77
     * @return string
78
     */
79 2
    private static function getRelativePath(string $path): string
80
    {
81 2
        return Util::getSubPathOf(Util::pathToArray($path), Util::pathToArray((string)getcwd()));
82
    }
83
}
84