Passed
Push — master ( 676110...9cfebc )
by Daniel
02:12
created

Releaser::doRelease()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Operation;
6
7
use Dandelion\Configuration\ConfigurationLoaderInterface;
8
use Dandelion\Configuration\Repository;
9
use Dandelion\Exception\RepositoryNotFoundException;
10
use Dandelion\Filesystem\FilesystemInterface;
11
use Dandelion\Process\ProcessFactory;
12
use Dandelion\VersionControl\GitInterface;
13
14
use function sprintf;
15
16
class Releaser implements ReleaserInterface
17
{
18
    /**
19
     * @var \Dandelion\Configuration\ConfigurationLoaderInterface
20
     */
21
    protected $configurationLoader;
22
23
    /**
24
     * @var \Dandelion\Filesystem\FilesystemInterface
25
     */
26
    protected $filesystem;
27
28
    /**
29
     * @var \Dandelion\Process\ProcessFactory
30
     */
31
    protected $processFactory;
32
33
    /**
34
     * @var \Dandelion\VersionControl\GitInterface
35
     */
36
    protected $git;
37
38
    /**
39
     * @var string
40
     */
41
    protected $binDir;
42
43
    /**
44
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
45
     * @param \Dandelion\Filesystem\FilesystemInterface $filesystem
46
     * @param \Dandelion\Process\ProcessFactory $processFactory
47
     * @param \Dandelion\VersionControl\GitInterface $git
48
     * @param string $binDir
49
     */
50
    public function __construct(
51
        ConfigurationLoaderInterface $configurationLoader,
52
        FilesystemInterface $filesystem,
53
        ProcessFactory $processFactory,
54
        GitInterface $git,
55
        string $binDir
56
    ) {
57
        $this->configurationLoader = $configurationLoader;
58
        $this->filesystem = $filesystem;
59
        $this->processFactory = $processFactory;
60
        $this->git = $git;
61
        $this->binDir = $binDir;
62
    }
63
64
    /**
65
     * @param string $repositoryName
66
     * @param string $branch
67
     *
68
     * @return \Dandelion\Operation\ReleaserInterface
69
     * @throws \Exception
70
     */
71
    public function release(
72
        string $repositoryName,
73
        string $branch
74
    ): ReleaserInterface {
75
        $configuration = $this->configurationLoader->load();
76
        $repositories = $configuration->getRepositories();
77
78
        if (!$repositories->offsetExists($repositoryName)) {
79
            throw new RepositoryNotFoundException(sprintf('Could not find repository "%s".', $repositoryName));
80
        }
81
82
        $repository = $repositories->offsetGet($repositoryName);
83
        $tempDirectory = rtrim($configuration->getPathToTempDirectory(), DIRECTORY_SEPARATOR)
84
            . DIRECTORY_SEPARATOR . $repositoryName . DIRECTORY_SEPARATOR;
85
86
        $currentWorkingDirectory = $this->filesystem->getCurrentWorkingDirectory();
87
        $this->filesystem->changeDirectory($tempDirectory);
88
89
        $this->doRelease($branch, $repository);
90
91
        $this->filesystem->changeDirectory($currentWorkingDirectory)
92
            ->removeDirectory($tempDirectory);
93
94
        return $this;
95
    }
96
97
    /**
98
     * @param string $branch
99
     * @param \Dandelion\Configuration\Repository $repository
100
     *
101
     * @return \Dandelion\Operation\ReleaserInterface
102
     */
103
    protected function doRelease(string $branch, Repository $repository): ReleaserInterface
104
    {
105
        $version = $repository->getVersion();
106
107
        $this->git->clone($repository->getUrl(), '.')
108
            ->checkout($branch);
109
110
        if ($this->git->describeClosestTag($version) !== null) {
111
            return $this;
112
        }
113
114
        $this->git->tag($version)
115
            ->pushWithTags('origin');
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string $branch
122
     *
123
     * @return \Dandelion\Operation\ReleaserInterface
124
     */
125
    public function releaseAll(string $branch): ReleaserInterface
126
    {
127
        $configuration = $this->configurationLoader->load();
128
129
        foreach ($configuration->getRepositories() as $repositoryName => $repository) {
130
            $this->releaseAsProcess($repositoryName, $branch);
131
        }
132
133
        return $this;
134
    }
135
136
    /**
137
     * @param string $repositoryName
138
     * @param string $branch
139
     *
140
     * @return \Dandelion\Operation\ReleaserInterface
141
     */
142
    protected function releaseAsProcess(
143
        string $repositoryName,
144
        string $branch
145
    ): ReleaserInterface {
146
        $command = $command = [
0 ignored issues
show
Unused Code introduced by
The assignment to $command is dead and can be removed.
Loading history...
147
            sprintf('%sdandelion', $this->binDir),
148
            'release',
149
            $repositoryName,
150
            $branch
151
        ];
152
153
        $process = $this->processFactory->create($command);
154
155
        $process->start();
156
157
        return $this;
158
    }
159
}
160