DummyRepo   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 18
c 0
b 0
f 0
dl 0
loc 97
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoot() 0 3 1
A getHookDir() 0 3 1
A getGitDir() 0 3 1
A __construct() 0 4 1
A setupRepo() 0 7 2
A hookExists() 0 3 1
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\Git;
13
14
use org\bovigo\vfs\vfsStream;
15
16
class DummyRepo
17
{
18
    /**
19
     * Path to fake git repository
20
     *
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * Default empty hook git dir structure
27
     *
28
     * @var array
29
     */
30
    private static $defaultStructure = [
31
        'config' => '# fake git config',
32
        'hooks'  => [
33
            'pre-commit.sample' => '# fake pre-commit sample file',
34
            'pre-push.sample'   => '# fake pre-push sample file',
35
        ]
36
    ];
37
38
    /**
39
     * Fake stream directory structure
40
     *
41
     * @var \org\bovigo\vfs\vfsStreamDirectory
42
     */
43
    private $repo;
44
45
    /**
46
     * DummyRepo constructor
47
     *
48
     * @param array $gitDir
49
     * @param array $files
50
     */
51
    public function __construct(array $gitDir = [], array $files = [])
52
    {
53
        $this->repo  = vfsStream::setup('root', null, $this->setupRepo($gitDir, $files));
54
        $this->path  = $this->repo->url();
55
    }
56
57
    /**
58
     * Return the fake directory structure starting at '/repo-name'
59
     *
60
     * @param  array $gitDir
61
     * @param  array $files
62
     * @return array
63
     */
64
    private function setupRepo(array $gitDir, array $files)
65
    {
66
        $dotGit = empty($gitDir) ? self::$defaultStructure : $gitDir;
67
68
        return array_merge(
69
            ['.git' => $dotGit],
70
            $files
71
        );
72
    }
73
74
    /**
75
     * Tells if a hook exists
76
     *
77
     * @param  string $hook
78
     * @return bool
79
     */
80
    public function hookExists(string $hook): bool
81
    {
82
        return $this->repo->hasChild('.git/hooks/' . $hook);
83
    }
84
85
    /**
86
     * Return the path to the fake repository
87
     *
88
     * @return string
89
     */
90
    public function getRoot()
91
    {
92
        return $this->path;
93
    }
94
95
    /**
96
     * Return path to fake .git dir
97
     *
98
     * @return string
99
     */
100
    public function getGitDir()
101
    {
102
        return $this->getRoot() . '/.git';
103
    }
104
105
    /**
106
     * Return path to the fake hook dir
107
     *
108
     * @return string
109
     */
110
    public function getHookDir()
111
    {
112
        return $this->getGitDir() . '/hooks';
113
    }
114
}
115