1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AurimasNiekis\GitConfig; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use AurimasNiekis\GitConfig\Exception\GitNotFoundException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Config |
10
|
|
|
* |
11
|
|
|
* @package AurimasNiekis\GitConfig |
12
|
|
|
* @author Aurimas Niekis <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Config |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $gitExecutable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $gitConfigFile; |
25
|
|
|
|
26
|
8 |
|
public function __construct(string $gitExecutable = null, string $gitConfigFile = null) |
27
|
|
|
{ |
28
|
8 |
|
if (null === $gitExecutable) { |
29
|
7 |
|
$exitCode = rtrim(shell_exec('which git > /dev/null; echo $?')); |
30
|
|
|
|
31
|
7 |
|
if ('0' !== $exitCode) { |
32
|
1 |
|
throw new GitNotFoundException(); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
1 |
|
$this->gitExecutable = $gitExecutable; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
$this->gitConfigFile = $gitConfigFile; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
7 |
View Code Duplication |
public function get(string $name, bool $global = false, bool $system = false, bool $allScopes = true) |
|
|
|
|
42
|
|
|
{ |
43
|
7 |
|
$command = $this->getScopeFlags($global, $system, $allScopes); |
44
|
|
|
|
45
|
4 |
|
$command .= ' --get ' . $name; |
46
|
|
|
|
47
|
4 |
|
return $this->executeCommand($command); |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
public function set(string $name, string $value, bool $global = false, bool $system = false) |
51
|
|
|
{ |
52
|
2 |
|
$command = $this->getScopeFlags($global, $system); |
53
|
|
|
|
54
|
2 |
|
$command .= ' ' . $name . ' "' . $value . '"'; |
55
|
|
|
|
56
|
2 |
|
return $this->executeCommand($command); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
View Code Duplication |
public function unSet(string $name, bool $global = false, bool $system = false) |
|
|
|
|
60
|
|
|
{ |
61
|
1 |
|
$command = $this->getScopeFlags($global, $system); |
62
|
|
|
|
63
|
1 |
|
$command .= ' --unset ' . $name; |
64
|
|
|
|
65
|
1 |
|
return $this->executeCommand($command); |
66
|
|
|
} |
67
|
|
|
|
68
|
7 |
|
private function getScopeFlags(bool $global = false, bool $system = false, bool $allScopes = null) |
69
|
|
|
{ |
70
|
7 |
|
$command = ''; |
71
|
|
|
|
72
|
7 |
|
$flags = 0; |
73
|
7 |
|
if (true === $global) { |
74
|
1 |
|
$flags++; |
75
|
|
|
|
76
|
1 |
|
$command .= ' --global'; |
77
|
|
|
} |
78
|
|
|
|
79
|
7 |
|
if (true === $system) { |
80
|
1 |
|
$flags++; |
81
|
|
|
|
82
|
1 |
|
$command .= ' --system'; |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
if (true === $allScopes) { |
86
|
6 |
|
$flags++; |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
if (1 < $flags || (null !== $allScopes && 0 === $flags)) { |
90
|
3 |
|
throw new InvalidArgumentException( |
91
|
3 |
|
'Only $global, $system or $allScopes could be "true" at same time' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
return $command; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
private function getGitExecutable(): string |
99
|
|
|
{ |
100
|
4 |
|
return $this->gitExecutable ?? 'git'; |
101
|
|
|
} |
102
|
|
|
|
103
|
4 |
|
private function getGitConfigFile(): string |
104
|
|
|
{ |
105
|
4 |
|
if (null !== $this->gitConfigFile) { |
106
|
3 |
|
return ' --file ' . $this->gitConfigFile; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
|
private function executeCommand(string $command): string |
113
|
|
|
{ |
114
|
4 |
|
return rtrim( |
115
|
|
|
shell_exec( |
116
|
|
|
sprintf( |
117
|
4 |
|
'%s config%s%s', |
118
|
4 |
|
$this->getGitExecutable(), |
119
|
4 |
|
$this->getGitConfigFile(), |
120
|
|
|
$command |
121
|
|
|
) |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.