Completed
Push — master ( 2b54e4...eaaf1a )
by Sebastian
02:53
created

Repository::isMerging()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.6666
cc 3
eloc 5
nc 3
nop 0
crap 3
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
namespace sebastianfeldmann\CaptainHook\Git;
11
12
/**
13
 * Class Repository
14
 *
15
 * @package CaptainHook
16
 * @author  Sebastian Feldmann <[email protected]>
17
 * @link    https://github.com/sebastianfeldmann/captainhook
18
 * @since   Class available since Release 0.9.0
19
 */
20
class Repository
21
{
22
    /**
23
     * Path to git repository root
24
     *
25
     * @var string
26
     */
27
    private $root;
28
29
    /**
30
     * Path to .git directory
31
     *
32
     * @var string
33
     */
34
    private $dotGitDir;
35
36
    /**
37
     * Commit message data
38
     *
39
     * @var \sebastianfeldmann\CaptainHook\Git\CommitMessage
40
     */
41
    private $commitMsg;
42
43
    /**
44
     * Resolver to get changed files
45
     *
46
     * @var \sebastianfeldmann\CaptainHook\Git\Resolver\ChangedFiles
47
     */
48
    private $changedFilesResolver;
49
50
    /**
51
     * Repository constructor.
52
     *
53
     * @param string $root
54
     */
55 28
    public function __construct($root = null)
56
    {
57 28
        $this->root      = null === $root ? getcwd() : $root;
58 28
        $this->dotGitDir = $this->root . DIRECTORY_SEPARATOR . '.git';
59
        // check if it's a valid git repository
60 28
        if (!is_dir($this->dotGitDir)) {
61 3
            throw new \RuntimeException('Invalid git repository path: ' . $this->root);
62
        }
63 25
    }
64
65
    /**
66
     * Root path getter.
67
     *
68
     * @return string
69
     */
70 2
    public function getRoot()
71
    {
72 2
        return $this->root;
73
    }
74
75
    /**
76
     * Returns the path to the hooks directory.
77
     *
78
     * @return string
79
     */
80 4
    public function getHooksDir()
81
    {
82 4
        return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks';
83
    }
84
85
    /**
86
     * Check for a hook file.
87
     *
88
     * @param  string $hook
89
     * @return bool
90
     */
91 3
    public function hookExists($hook)
92
    {
93 3
        return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
94
    }
95
96
    /**
97
     * CommitMessage setter.
98
     *
99
     * @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $commitMsg
100
     */
101 8
    public function setCommitMsg(CommitMessage $commitMsg)
102
    {
103 8
        $this->commitMsg = $commitMsg;
104 8
    }
105
106
    /**
107
     * CommitMessage getter.
108
     *
109
     * @return \sebastianfeldmann\CaptainHook\Git\CommitMessage
110
     */
111 7
    public function getCommitMsg()
112
    {
113 7
        if (null === $this->commitMsg) {
114 1
            throw new \RuntimeException('No commit message available');
115
        }
116 6
        return $this->commitMsg;
117
    }
118
119
    /**
120
     * Get changed file resolver.
121
     *
122
     * @return \sebastianfeldmann\CaptainHook\Git\Resolver\ChangedFiles
123
     */
124 1
    public function getChangedFilesResolver()
125
    {
126 1
        if (null === $this->changedFilesResolver) {
127 1
            $this->changedFilesResolver = new Resolver\ChangedFiles();
128
        }
129 1
        return $this->changedFilesResolver;
130
    }
131
132
    /**
133
     * Is there a merge in progress.
134
     *
135
     * @return bool
136
     */
137 4
    public function isMerging()
138
    {
139 4
        foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) {
140 4
            if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) {
141 4
                return true;
142
            }
143
        }
144 4
        return false;
145
    }
146
}
147