Completed
Push — master ( 53c17e...63d775 )
by Sebastian
02:56
created

Repository::getChangedFilesResolver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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