Git   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
eloc 19
c 6
b 2
f 1
dl 0
loc 56
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 11 4
A addRemote() 0 5 1
A loadConfig() 0 14 2
A init() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the PHINT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc\Phint\Util;
13
14
class Git extends Executable
15
{
16
    /** @var array */
17
    protected $gitConfig;
18
19
    /** @var string The binary executable */
20
    protected $binary = 'git';
21
22
    /**
23
     * Gets git config.
24
     *
25
     * @param string|null $key
26
     *
27
     * @return mixed
28
     */
29
    public function getConfig($key = null)
30
    {
31
        if (null === $this->gitConfig) {
32
            $this->loadConfig();
33
        }
34
35
        if (null === $key) {
36
            return $this->gitConfig;
37
        }
38
39
        return isset($this->gitConfig[$key]) ? $this->gitConfig[$key] : null;
40
    }
41
42
    protected function loadConfig()
43
    {
44
        $gitConfig = [];
45
46
        $output = $this->runCommand('config --list');
47
        $output = explode("\n", str_replace(["\r\n", "\r"], "\n", $output));
48
49
        foreach ($output as $config) {
50
            $parts = array_map('trim', explode('=', $config, 2)) + ['', ''];
51
52
            $gitConfig[$parts[0]] = $parts[1];
53
        }
54
55
        $this->gitConfig = $gitConfig;
56
    }
57
58
    public function init()
59
    {
60
        $this->runCommand('init');
61
62
        return $this;
63
    }
64
65
    public function addRemote($username, $project)
66
    {
67
        $this->runCommand(sprintf('remote add origin [email protected]:%s/%s.git', $username, $project));
68
69
        return $this;
70
    }
71
}
72