Completed
Push — master ( 91746d...c63af8 )
by ReliQ
09:16
created

GitCommandRunner::getRemoteUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Service;
6
7
use ReliqArts\Docweaver\Contract\VCSCommandRunner;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
final class GitCommandRunner implements VCSCommandRunner
12
{
13
    private const DEFAULT_REMOTE_NAME = 'origin';
14
15
    /**
16
     * @param string $source
17
     * @param string $branch
18
     * @param string $workingDirectory
19
     */
20
    public function clone(string $source, string $branch, string $workingDirectory): void
21
    {
22
        $command = ['git', 'clone', '--branch', $branch, sprintf('%s', $source), $branch];
23
24
        $clone = new Process($command, $workingDirectory);
25
        $clone->mustRun();
26
    }
27
28
    /**
29
     * @param string $workingDirectory
30
     *
31
     * @throws ProcessFailedException
32
     *
33
     * @return array
34
     */
35
    public function listTags(string $workingDirectory): array
36
    {
37
        $this->fetch($workingDirectory);
38
39
        $listTags = new Process(['git', 'tag', '-l'], $workingDirectory);
40
        $listTags->mustRun();
41
42
        if ($splitTags = preg_split("/[\n\r]/", $listTags->getOutput())) {
43
            return array_filter(array_map('trim', $splitTags));
44
        }
45
46
        return [];
47
    }
48
49
    /**
50
     * @param string $workingDirectory
51
     *
52
     * @throws ProcessFailedException
53
     */
54
    public function pull(string $workingDirectory): void
55
    {
56
        $pull = new Process(['git', 'pull'], $workingDirectory);
57
        $pull->mustRun();
58
    }
59
60
    /**
61
     * @param string      $workingDirectory
62
     * @param null|string $remoteName
63
     *
64
     * @throws ProcessFailedException
65
     *
66
     * @return string
67
     */
68
    public function getRemoteUrl(string $workingDirectory, ?string $remoteName = null): string
69
    {
70
        $remoteName = $remoteName ?? self::DEFAULT_REMOTE_NAME;
71
        $command = ['git', 'config', '--get', sprintf('remote.%s.url', $remoteName)];
72
73
        $getUrl = new Process($command, $workingDirectory);
74
        $getUrl->mustRun();
75
76
        return trim($getUrl->getOutput());
77
    }
78
79
    /**
80
     * @param string $workingDirectory
81
     */
82
    private function fetch(string $workingDirectory): void
83
    {
84
        $pull = new Process(['git', 'fetch'], $workingDirectory);
85
        $pull->mustRun();
86
    }
87
}
88