Skip to content

Commit 4a01197

Browse files
committed
Create service to clone/pull repos
1 parent a4662a6 commit 4a01197

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"roave/better-reflection": "^4.12",
88
"phpdocumentor/reflection-docblock": "^5.2",
99
"ext-simplexml": "*",
10-
"symfony/filesystem": "^5.2"
10+
"symfony/filesystem": "^5.2",
11+
"symfony/process": "^5.2"
1112
},
1213
"autoload": {
1314
"psr-4": {

composer.lock

+63-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/CodeRepository/CodeRepository.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\CodeRepository;
5+
6+
interface CodeRepository
7+
{
8+
public const CODE_DOWNLOAD_PATH = 'var/repos/';
9+
10+
public function downloadCode(string $repoName): string;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\CodeRepository;
5+
6+
use Symfony\Component\Process\Exception\ProcessFailedException;
7+
use Symfony\Component\Process\Process;
8+
9+
class GithubCodeRepository implements CodeRepository
10+
{
11+
public function downloadCode(string $repoName): string
12+
{
13+
$repoPath = self::CODE_DOWNLOAD_PATH . '/' . $repoName;
14+
15+
$process = $this->cloneOrPull($repoPath, $repoName);
16+
$process->run();
17+
18+
if (!$process->isSuccessful()) {
19+
throw new ProcessFailedException($process);
20+
}
21+
22+
return $repoPath;
23+
}
24+
25+
private function cloneOrPull(string $repoPath, string $repoName): Process
26+
{
27+
if (!is_dir($repoPath)) {
28+
return new Process([
29+
'git',
30+
'clone',
31+
'[email protected]:' . $repoName,
32+
$repoPath
33+
]);
34+
}
35+
36+
return new Process([
37+
'git',
38+
'-C',
39+
$repoPath,
40+
'pull'
41+
]);
42+
}
43+
}

0 commit comments

Comments
 (0)