Completed
Push — master ( 63d775...2b54e4 )
by Sebastian
02:42
created

Repository::getRoot()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 4
    public function getRoot()
71
    {
72 4
        return $this->root;
73
    }
74
75
    /**
76
     * Returns the path to the hooks directory.
77
     *
78
     * @return string
79
     */
80
    public function getHooksDir()
81 3
    {
82
        return $this->dotGitDir . DIRECTORY_SEPARATOR . 'hooks';
83 3
    }
84
85
    /**
86
     * Check for a hook file.
87
     *
88
     * @param  string $hook
89
     * @return bool
90
     */
91 8
    public function hookExists($hook)
92
    {
93 8
        return file_exists($this->getHooksDir() . DIRECTORY_SEPARATOR . $hook);
94 8
    }
95
96
    /**
97
     * CommitMessage setter.
98
     *
99
     * @param \sebastianfeldmann\CaptainHook\Git\CommitMessage $commitMsg
100
     */
101 7
    public function setCommitMsg(CommitMessage $commitMsg)
102
    {
103 7
        $this->commitMsg = $commitMsg;
104 1
    }
105
106 6
    /**
107
     * CommitMessage getter.
108
     *
109
     * @return \sebastianfeldmann\CaptainHook\Git\CommitMessage
110
     */
111
    public function getCommitMsg()
112
    {
113
        if (null === $this->commitMsg) {
114 1
            throw new \RuntimeException('No commit message available');
115
        }
116 1
        return $this->commitMsg;
117 1
    }
118
119 1
    /**
120
     * Get changed file resolver.
121
     *
122
     * @return \sebastianfeldmann\CaptainHook\Git\Resolver\ChangedFiles
123
     */
124
    public function getChangedFilesResolver()
125
    {
126
        if (null === $this->changedFilesResolver) {
127 4
            $this->changedFilesResolver = new Resolver\ChangedFiles();
128
        }
129 4
        return $this->changedFilesResolver;
130 4
    }
131 4
132
    /**
133
     * Is there a merge in progress.
134 4
     *
135
     * @return bool
136
     */
137
    public function isMerging()
138
    {
139
        foreach (['MERGE_MSG', 'MERGE_HEAD', 'MERGE_MODE'] as $fileName) {
140
            if (file_exists($this->dotGitDir . DIRECTORY_SEPARATOR . $fileName)) {
141
                return true;
142
            }
143
        }
144
        return false;
145
    }
146
}
147