Completed
Push — master ( 9cfebc...41991d )
by Daniel
03:48
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\Console\Command\ReleaseCommand;
10
use Dandelion\Exception\RepositoryNotFoundException;
11
use Dandelion\Filesystem\FilesystemInterface;
12
use Dandelion\Operation\Result\MessageFactoryInterface;
13
use Dandelion\Process\ProcessPoolFactoryInterface;
14
use Dandelion\VersionControl\GitInterface;
15
16
use function sprintf;
17
18
class Releaser extends AbstractOperation
19
{
20
    /**
21
     * @var \Dandelion\Filesystem\FilesystemInterface
22
     */
23
    protected $filesystem;
24
25
    /**
26
     * @var \Dandelion\VersionControl\GitInterface
27
     */
28
    protected $git;
29
30
    /**
31
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
32
     * @param \Dandelion\Filesystem\FilesystemInterface $filesystem
33
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
34
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
35
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
36
     * @param \Dandelion\VersionControl\GitInterface $git
37
     * @param string $binDir
38
     */
39
    public function __construct(
40
        ConfigurationLoaderInterface $configurationLoader,
41
        FilesystemInterface $filesystem,
42
        ProcessPoolFactoryInterface $processPoolFactory,
43
        ResultFactoryInterface $resultFactory,
44
        MessageFactoryInterface $messageFactory,
45
        GitInterface $git,
46
        string $binDir
47
    ) {
48
        parent::__construct($configurationLoader, $processPoolFactory, $resultFactory, $messageFactory, $binDir);
49
        $this->filesystem = $filesystem;
50
        $this->git = $git;
51
    }
52
53
    /**
54
     * @param string $repositoryName
55
     * @param string $branch
56
     *
57
     * @return \Dandelion\Operation\AbstractOperation
58
     *
59
     * @throws \Dandelion\Exception\RepositoryNotFoundException
60
     */
61
    public function executeForSingleRepository(
62
        string $repositoryName,
63
        string $branch
64
    ): AbstractOperation {
65
        $configuration = $this->configurationLoader->load();
66
        $repositories = $configuration->getRepositories();
67
68
        if (!$repositories->offsetExists($repositoryName)) {
69
            throw new RepositoryNotFoundException(sprintf('Could not find repository "%s".', $repositoryName));
70
        }
71
72
        $repository = $repositories->offsetGet($repositoryName);
73
        $tempDirectory = rtrim($configuration->getPathToTempDirectory(), DIRECTORY_SEPARATOR)
74
            . DIRECTORY_SEPARATOR . $repositoryName . DIRECTORY_SEPARATOR;
75
76
        $currentWorkingDirectory = $this->filesystem->getCurrentWorkingDirectory();
77
        $this->filesystem->changeDirectory($tempDirectory);
78
79
        $this->doExecuteForSingleRepository($branch, $repository);
80
81
        $this->filesystem->changeDirectory($currentWorkingDirectory)
82
            ->removeDirectory($tempDirectory);
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $branch
89
     * @param \Dandelion\Configuration\Repository $repository
90
     *
91
     * @return \Dandelion\Operation\AbstractOperation
92
     */
93
    protected function doExecuteForSingleRepository(string $branch, Repository $repository): AbstractOperation
94
    {
95
        $version = $repository->getVersion();
96
97
        $this->git->clone($repository->getUrl(), '.')
98
            ->checkout($branch);
99
100
        if ($this->git->describeClosestTag($version) !== null) {
101
            return $this;
102
        }
103
104
        $this->git->tag($version)
105
            ->pushWithTags('origin');
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $repositoryName
112
     * @param string $branch
113
     *
114
     * @return string[]
115
     */
116
    protected function getCommand(string $repositoryName, string $branch): array
117
    {
118
        return [
119
            sprintf('%sdandelion', $this->binDir),
120
            ReleaseCommand::NAME,
121
            $repositoryName,
122
            $branch
123
        ];
124
    }
125
}
126