Completed
Pull Request — master (#1)
by Billie
01:48
created

Repository   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 3
cbo 1
dl 0
loc 172
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A getRoot() 0 4 1
A getHooksDir() 0 4 1
A hookExists() 0 4 1
A setCommitMsg() 0 4 1
A getCommitMsg() 0 8 2
A isMerging() 0 10 3
A getIndexOperator() 0 4 1
A getLogOperator() 0 4 1
A getConfigOperator() 0 4 1
A getOperator() 0 9 2
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Git.
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
namespace SebastianFeldmann\Git;
11
12
use SebastianFeldmann\Cli\Command\Runner;
13
14
/**
15
 * Class Repository
16
 *
17
 * @package SebastianFeldmann\Git
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/git
20
 * @since   Class available since Release 0.9.0
21
 */
22
class Repository
23
{
24
    /**
25
     * Path to git repository root.
26
     *
27
     * @var string
28
     */
29
    private $root;
30
31
    /**
32
     * Path to .git directory
33
     *
34
     * @var string
35
     */
36
    private $dotGitDir;
37
38
    /**
39
     * Commit message.
40
     *
41
     * @var \SebastianFeldmann\Git\CommitMessage
42
     */
43
    private $commitMsg;
44
45
    /**
46
     * @var \SebastianFeldmann\Cli\Command\Runner
47
     */
48
    private $runner;
49
50
    /**
51
     * Map of operators
52
     *
53
     * @var array
54
     */
55
    private $operator = [];
56
57
    /**
58
     * Repository constructor.
59
     *
60
     * @param string                                $root
61
     * @param \SebastianFeldmann\Cli\Command\Runner $runner
62
     */
63 10
    public function __construct(string $root = '', Runner $runner = null)
64
    {
65 10
        $path = empty($root) ? getcwd() : realpath($root);
66
        // check for existing .git dir
67 10
        if (!is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
68 1
            throw new \RuntimeException(sprintf('Invalid git repository: %s', $root));
69
        }
70 9
        $this->root      = $path;
71 9
        $this->dotGitDir = $this->root.DIRECTORY_SEPARATOR.'.git';
72 9
        $this->runner    = null == $runner ? new Runner\Simple() : $runner;
73 9
    }
74
75
    /**
76
     * Root path getter.
77
     *
78
     * @return string
79
     */
80 1
    public function getRoot() : string
81
    {
82 1
        return $this->root;
83
    }
84
85
    /**
86
     * Returns the path to the hooks directory.
87
     *
88
     * @return string
89
     */
90 2
    public function getHooksDir() : string
91
    {
92 2
        return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks';
93
    }
94
95
    /**
96
     * Check for a hook file.
97
     *
98
     * @param  string $hook
99
     *
100
     * @return bool
101
     */
102 1
    public function hookExists($hook) : bool
103
    {
104 1
        return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
105
    }
106
107
    /**
108
     * CommitMessage setter.
109
     *
110
     * @param \SebastianFeldmann\Git\CommitMessage $commitMsg
111
     */
112 1
    public function setCommitMsg(CommitMessage $commitMsg)
113
    {
114 1
        $this->commitMsg = $commitMsg;
115 1
    }
116
117
    /**
118
     * CommitMessage getter.
119
     *
120
     * @return \SebastianFeldmann\Git\CommitMessage
121
     */
122 2
    public function getCommitMsg() : CommitMessage
123
    {
124 2
        if (null === $this->commitMsg) {
125 1
            throw new \RuntimeException('No commit message available');
126
        }
127
128 1
        return $this->commitMsg;
129
    }
130
131
    /**
132
     * Is there a merge in progress.
133
     *
134
     * @return bool
135
     */
136 2
    public function isMerging() : bool
137
    {
138 2
        foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) {
139 2
            if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) {
140 2
                return true;
141
            }
142
        }
143
144 1
        return false;
145
    }
146
147
    /**
148
     * Get index operator.
149
     *
150
     * @return \SebastianFeldmann\Git\Operator\Index
151
     */
152 1
    public function getIndexOperator() : Operator\Index
153
    {
154 1
        return $this->getOperator('Index');
155
    }
156
157
    /**
158
     * Get log operator.
159
     *
160
     * @return \SebastianFeldmann\Git\Operator\Log
161
     */
162 1
    public function getLogOperator() : Operator\Log
163
    {
164 1
        return $this->getOperator('Log');
165
    }
166
167
    /**
168
     * Get config operator.
169
     *
170
     * @return \SebastianFeldmann\Git\Operator\Config
171
     */
172 1
    public function getConfigOperator() : Operator\Config
173
    {
174 1
        return $this->getOperator('Config');
175
    }
176
177
    /**
178
     * Return requested operator.
179
     *
180
     * @param  string $name
181
     *
182
     * @return mixed
183
     */
184 3
    private function getOperator(string $name)
185
    {
186 3
        if (!isset($this->operator[$name])) {
187 3
            $class = '\\SebastianFeldmann\\Git\\Operator\\' . $name;
188 3
            $this->operator[$name] = new $class($this->runner, $this);
189
        }
190
191 3
        return $this->operator[$name];
192
    }
193
}
194