Passed
Push — master ( a467a6...de53c5 )
by Sebastian
02:03
created

Docker   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 7
dl 0
loc 39
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCode() 0 4 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
 * Docker class
19
 *
20
 * Generates the bash scripts placed in .git/hooks/* for every hook
21
 * to execute CaptainHook inside of a Docker container.
22
 *
23
 * @package CaptainHook
24
 * @author  Sebastian Feldmann <[email protected]>
25
 * @link    https://github.com/captainhookphp/captainhook
26
 * @since   Class available since Release 4.3.0
27
 */
28
class Docker implements Template
29
{
30
    /**
31
     * Path to the captainhook-run binary
32
     *
33
     * @var string
34
     */
35
    private $binaryPath;
36
37
    /**
38
     * Name of the container to spin up
39
     *
40
     * @var string
41
     */
42
    private $containerName;
43
44
    /**
45
     * Docker constructor
46
     *
47
     * @param string $repoPath
48
     * @param string $vendorPath
49
     * @param string $containerName
50
     */
51 2
    public function __construct(string $repoPath, string $vendorPath, string $containerName)
52
    {
53 2
        $this->binaryPath    = Util::getBinaryPath($repoPath, $vendorPath, 'captainhook-run');
54 2
        $this->containerName = $containerName;
55 2
    }
56
57
    /**
58
     * Return the code for the git hook scripts
59
     *
60
     * @param  string $hook Name of the hook to generate the sourcecode for
61
     * @return string
62
     */
63 2
    public function getCode(string $hook): string
64
    {
65 2
        return '#!/usr/bin/env bash' . PHP_EOL .
66 2
            'docker exec ' . $this->containerName . ' ./' . $this->binaryPath . ' ' . $hook . ' "$@"';
67
    }
68
}
69