Completed
Push — master ( dfd004...b46837 )
by Jitendra
11s
created

Git::runCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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