1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Gitilicious\GitClient\Git; |
4
|
|
|
|
5
|
|
|
use Gitilicious\GitClient\Cli\Input\LongFlag; |
6
|
|
|
use Gitilicious\GitClient\Cli\Input\PlainArgument; |
7
|
|
|
use Gitilicious\GitClient\Cli\Input\ShortFlag; |
8
|
|
|
use Gitilicious\GitClient\Client; |
9
|
|
|
use Gitilicious\GitClient\FileSystem\Directory; |
10
|
|
|
use Gitilicious\GitClient\FileSystem\File; |
11
|
|
|
|
12
|
|
|
class Repository |
13
|
|
|
{ |
14
|
|
|
private $client; |
15
|
|
|
|
16
|
|
|
private $directory; |
17
|
|
|
|
18
|
|
|
private $name; |
19
|
|
|
|
20
|
10 |
|
public function __construct(Client $client, Directory $directory, string $name) |
21
|
|
|
{ |
22
|
10 |
|
$this->client = $client; |
23
|
10 |
|
$this->directory = $directory; |
24
|
10 |
|
$this->name = $name; |
25
|
10 |
|
} |
26
|
|
|
|
27
|
4 |
View Code Duplication |
public static function createBare(Client $client, Directory $directory, string $name): Repository |
|
|
|
|
28
|
|
|
{ |
29
|
4 |
|
$repositoryDirectory = $directory->create($directory->getPath() . '/' . $name); |
30
|
|
|
|
31
|
4 |
|
$result = $client->run($repositoryDirectory, new PlainArgument('init'), new LongFlag('bare')); |
32
|
|
|
|
33
|
4 |
|
if (!$result->isSuccess()) { |
34
|
1 |
|
throw new Exception($result->getErrorMessage()); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return new self($client, $directory, $name); |
38
|
|
|
} |
39
|
|
|
|
40
|
8 |
View Code Duplication |
public static function create(Client $client, Directory $directory, string $name): Repository |
|
|
|
|
41
|
|
|
{ |
42
|
8 |
|
$repositoryDirectory = $directory->create($directory->getPath() . '/' . $name); |
43
|
|
|
|
44
|
8 |
|
$result = $client->run($repositoryDirectory, new PlainArgument('init')); |
45
|
|
|
|
46
|
8 |
|
if (!$result->isSuccess()) { |
47
|
1 |
|
throw new Exception($result->getErrorMessage()); |
48
|
|
|
} |
49
|
|
|
|
50
|
7 |
|
return new self($client, $directory, $name); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
public function clone(Directory $directory, string $name): Repository |
54
|
|
|
{ |
55
|
3 |
|
$repositoryDirectory = $directory->create($directory->getPath() . '/' . $name); |
56
|
|
|
|
57
|
3 |
|
$result = $this->client->run( |
58
|
|
|
$repositoryDirectory, |
59
|
3 |
|
new PlainArgument('clone'), |
60
|
3 |
|
new PlainArgument($this->getPath()), |
61
|
3 |
|
new PlainArgument($repositoryDirectory->getPath()) |
62
|
|
|
); |
63
|
|
|
|
64
|
3 |
|
if (!$result->isSuccess()) { |
65
|
1 |
|
throw new Exception($result->getErrorMessage()); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
return new self($this->client, $directory, $name); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
public function getPath(): string |
72
|
|
|
{ |
73
|
4 |
|
return $this->directory->getPath() . '/' . $this->name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getBranches(): Branches |
77
|
|
|
{ |
78
|
|
|
$result = $this->client->run($this->getPath(), 'branch'); |
79
|
|
|
|
80
|
|
|
if (!$result->isSuccess()) { |
81
|
|
|
throw new Exception('Cannot list the branches.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$branches = new Branches($this->client); |
|
|
|
|
85
|
|
|
|
86
|
|
|
foreach ($result->getOutput() as $branch) { |
|
|
|
|
87
|
|
|
$branches->add(new Branch($branch)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $branches; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function add(File $file) |
94
|
|
|
{ |
95
|
|
|
$this->client->run($this->directory, new PlainArgument('add'), new PlainArgument($file->getPath())); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function commit(Author $author, string $message) |
99
|
|
|
{ |
100
|
|
|
$this->client->run( |
101
|
|
|
$this->directory, |
102
|
|
|
new PlainArgument('commit'), |
103
|
|
|
new ShortFlag('m'), |
104
|
|
|
new PlainArgument($message), |
105
|
|
|
new LongFlag('author'), |
106
|
|
|
new PlainArgument($author->getFormatted()) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function push() |
111
|
|
|
{ |
112
|
|
|
$this->client->run($this->directory, new PlainArgument('push')); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.