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

Repository::getConfigOperator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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