Releaser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 37
dl 0
loc 120
rs 10
c 1
b 0
f 1
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommand() 0 8 1
A __construct() 0 18 1
A executeForSingleRepository() 0 27 2
A doExecuteForSingleRepository() 0 19 2
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
use Dandelion\VersionControl\Platform\PlatformFactoryInterface;
16
use Dandelion\VersionControl\Platform\PlatformInterface;
17
18
use function sprintf;
19
20
class Releaser extends AbstractOperation implements ReleaserInterface
21
{
22
    /**
23
     * @var \Dandelion\Filesystem\FilesystemInterface
24
     */
25
    protected $filesystem;
26
27
    /**
28
     * @var \Dandelion\VersionControl\GitInterface
29
     */
30
    protected $git;
31
32
    /**
33
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
34
     * @param \Dandelion\Filesystem\FilesystemInterface $filesystem
35
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
36
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
37
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
38
     * @param \Dandelion\VersionControl\Platform\PlatformFactoryInterface $platformFactory
39
     * @param \Dandelion\VersionControl\GitInterface $git
40
     */
41
    public function __construct(
42
        ConfigurationLoaderInterface $configurationLoader,
43
        FilesystemInterface $filesystem,
44
        ProcessPoolFactoryInterface $processPoolFactory,
45
        ResultFactoryInterface $resultFactory,
46
        MessageFactoryInterface $messageFactory,
47
        PlatformFactoryInterface $platformFactory,
48
        GitInterface $git
49
    ) {
50
        parent::__construct(
51
            $configurationLoader,
52
            $processPoolFactory,
53
            $resultFactory,
54
            $messageFactory,
55
            $platformFactory
56
        );
57
        $this->filesystem = $filesystem;
58
        $this->git = $git;
59
    }
60
61
    /**
62
     * @param string $repositoryName
63
     * @param string $branch
64
     *
65
     * @return \Dandelion\Operation\ReleaserInterface
66
     *
67
     * @throws \Dandelion\Exception\RepositoryNotFoundException
68
     */
69
    public function executeForSingleRepository(
70
        string $repositoryName,
71
        string $branch
72
    ): ReleaserInterface {
73
        $configuration = $this->configurationLoader->load();
74
        $repositories = $configuration->getRepositories();
75
76
        if (!$repositories->offsetExists($repositoryName)) {
77
            throw new RepositoryNotFoundException(sprintf('Could not find repository "%s".', $repositoryName));
78
        }
79
80
        $repository = $repositories->offsetGet($repositoryName);
81
        $tempDirectory = rtrim($configuration->getPathToTempDirectory(), DIRECTORY_SEPARATOR)
82
            . DIRECTORY_SEPARATOR . $repositoryName . DIRECTORY_SEPARATOR;
83
84
        $this->filesystem->createDirectory($tempDirectory);
85
        $currentWorkingDirectory = $this->filesystem->getCurrentWorkingDirectory();
86
        $this->filesystem->changeDirectory($tempDirectory);
87
88
        $platform = $this->platformFactory->create($configuration->getVcs());
89
90
        $this->doExecuteForSingleRepository($platform, $branch, $repository, $repositoryName);
91
92
        $this->filesystem->changeDirectory($currentWorkingDirectory)
93
            ->removeDirectory($tempDirectory);
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param \Dandelion\VersionControl\Platform\PlatformInterface $platform
100
     * @param string $branch
101
     * @param \Dandelion\Configuration\Repository $repository
102
     * @param string $repositoryName
103
     *
104
     * @return \Dandelion\Operation\AbstractOperation
105
     */
106
    protected function doExecuteForSingleRepository(
107
        PlatformInterface $platform,
108
        string $branch,
109
        Repository $repository,
110
        string $repositoryName
111
    ): AbstractOperation {
112
        $version = $repository->getVersion();
113
114
        $this->git->clone($platform->getRepositoryUrl($repositoryName), '.')
115
            ->checkout($branch);
116
117
        if ($this->git->describeClosestTag($version) !== null) {
118
            return $this;
119
        }
120
121
        $this->git->tag($version)
122
            ->pushWithTags('origin');
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param string[] $commandArguments
129
     *
130
     * @return string[]
131
     */
132
    protected function getCommand(array $commandArguments): array
133
    {
134
        return array_merge(
135
            [
136
                DANDELION_BINARY,
137
                ReleaseCommand::NAME,
138
            ],
139
            $commandArguments
140
        );
141
    }
142
}
143