Passed
Push — master ( b34030...56c38f )
by Sebastian
03:50
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
     * Repository constructor.
45
     *
46
     * @param string $root
47
     */
48 19
    public function __construct($root = null)
49
    {
50 19
        $this->root      = null === $root ? getcwd() : $root;
51 19
        $this->dotGitDir = $this->root . DIRECTORY_SEPARATOR . '.git';
52
        // check if it's a valid git repository
53 19
        if (!is_dir($this->dotGitDir)) {
54 2
            throw new \RuntimeException('Invalid git repository path: ' . $this->root);
55
        }
56 17
    }
57
58
    /**
59
     * Returns the path to the hooks directory.
60
     *
61
     * @return string
62
     */
63 3
    public function getHooksDir()
64
    {
65 3
        return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks';
66
    }
67
68
    /**
69
     * Check for a hook file.
70
     *
71
     * @param  string $hook
72
     * @return bool
73
     */
74 2
    public function hookExists($hook)
75
    {
76 2
        return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
77
    }
78
79
    /**
80
     * CommitMessage setter.
81
     *
82
     * @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $commitMsg
83
     */
84 6
    public function setCommitMsg(CommitMessage $commitMsg)
85
    {
86 6
        $this->commitMsg = $commitMsg;
87 6
    }
88
89
    /**
90
     * CommitMessage getter.
91
     *
92
     * @return \sebastianfeldmann\CaptainHook\Git\CommitMessage
93
     */
94 5
    public function getCommitMsg()
95
    {
96 5
        if (null === $this->commitMsg) {
97 1
            throw new \RuntimeException('No commit message available');
98
        }
99 4
        return $this->commitMsg;
100
    }
101
102
    /**
103
     * Is there a merge in progress.
104
     *
105
     * @return bool
106
     */
107 4
    public function isMerging()
108
    {
109 4
        foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) {
110 4
            if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) {
111 4
                return true;
112
            }
113
        }
114 4
        return false;
115
    }
116
}
117