Completed
Pull Request — master (#9)
by Jitendra
01:58
created

Git::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Ahc\Phint\Util;
4
5
class Git extends Executable
6
{
7
    /** @var array */
8
    protected $gitConfig;
9
10
    /** @var string The binary executable */
11
    protected $binary = 'git';
12
13
    /**
14
     * Gets git config.
15
     *
16
     * @param string|null $key
17
     *
18
     * @return mixed
19
     */
20
    public function getConfig($key = null)
21
    {
22
        if (null === $this->gitConfig) {
23
            $this->loadConfig();
24
        }
25
26
        if (null === $key) {
27
            return $this->gitConfig;
28
        }
29
30
        return isset($this->gitConfig[$key]) ? $this->gitConfig[$key] : null;
31
    }
32
33
    protected function loadConfig()
34
    {
35
        $gitConfig = [];
36
37
        $output = $this->runCommand('config --list');
38
        $output = explode("\n", str_replace(["\r\n", "\r"], "\n", $output));
39
40
        foreach ($output as $config) {
41
            $parts = array_map('trim', explode('=', $config, 2)) + ['', ''];
42
43
            $gitConfig[$parts[0]] = $parts[1];
44
        }
45
46
        $this->gitConfig = $gitConfig;
47
    }
48
49
    public function init()
50
    {
51
        $this->runCommand('init');
52
53
        return $this;
54
    }
55
56
    public function addRemote($username, $project)
57
    {
58
        $this->runCommand(sprintf('remote add origin [email protected]:%s/%s.git', $username, $project));
59
60
        return $this;
61
    }
62
}
63