Splitter::getCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Operation;
6
7
use Dandelion\Configuration\ConfigurationLoaderInterface;
8
use Dandelion\Console\Command\SplitCommand;
9
use Dandelion\Exception\RepositoryNotFoundException;
10
use Dandelion\Lock\LockTrait;
11
use Dandelion\Operation\Result\MessageFactoryInterface;
12
use Dandelion\Process\ProcessPoolFactoryInterface;
13
use Dandelion\VersionControl\GitInterface;
14
use Dandelion\VersionControl\Platform\PlatformFactoryInterface;
15
use Dandelion\VersionControl\SplitshLiteInterface;
16
use Symfony\Component\Lock\LockFactory;
17
18
use function sprintf;
19
20
class Splitter extends AbstractOperation implements SplitterInterface
21
{
22
    use LockTrait;
23
24
    public const LOCK_IDENTIFIER = 'LOCK_SPLIT';
25
26
    /**
27
     * @var \Dandelion\VersionControl\GitInterface
28
     */
29
    protected $git;
30
31
    /**
32
     * @var \Dandelion\VersionControl\SplitshLiteInterface
33
     */
34
    protected $splitshLite;
35
36
    /**
37
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
38
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
39
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
40
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
41
     * @param \Dandelion\VersionControl\Platform\PlatformFactoryInterface $platformFactory
42
     * @param \Dandelion\VersionControl\GitInterface $git
43
     * @param \Dandelion\VersionControl\SplitshLiteInterface $splitshLite
44
     * @param \Symfony\Component\Lock\LockFactory $lockFactory
45
     */
46
    public function __construct(
47
        ConfigurationLoaderInterface $configurationLoader,
48
        ProcessPoolFactoryInterface $processPoolFactory,
49
        ResultFactoryInterface $resultFactory,
50
        MessageFactoryInterface $messageFactory,
51
        PlatformFactoryInterface $platformFactory,
52
        GitInterface $git,
53
        SplitshLiteInterface $splitshLite,
54
        LockFactory $lockFactory
55
    ) {
56
        parent::__construct(
57
            $configurationLoader,
58
            $processPoolFactory,
59
            $resultFactory,
60
            $messageFactory,
61
            $platformFactory
62
        );
63
64
        $this->git = $git;
65
        $this->splitshLite = $splitshLite;
66
        $this->lockFactory = $lockFactory;
67
    }
68
69
    /**
70
     * @param string $repositoryName
71
     * @param string $branch
72
     *
73
     * @return \Dandelion\Operation\SplitterInterface
74
     *
75
     * @throws \Dandelion\Exception\RepositoryNotFoundException
76
     */
77
    public function executeForSingleRepository(string $repositoryName, string $branch): SplitterInterface
78
    {
79
        $configuration = $this->configurationLoader->load();
80
        $repositories = $configuration->getRepositories();
81
82
        if (!$repositories->offsetExists($repositoryName)) {
83
            throw new RepositoryNotFoundException(\sprintf('Could not find repository "%s".', $repositoryName));
84
        }
85
86
        $repository = $repositories->offsetGet($repositoryName);
87
        $platform = $this->platformFactory->create($configuration->getVcs());
88
89
        $this->acquire(static::LOCK_IDENTIFIER);
90
91
        if (!$this->git->existsRemote($repositoryName)) {
92
            $this->git->addRemote($repositoryName, $platform->getRepositoryUrl($repositoryName));
93
        }
94
95
        $sha1 = $this->splitshLite->getSha1($repository->getPath());
96
        $refSpec = sprintf('%s:refs/heads/%s', $sha1, $branch);
97
98
        $this->git->pushForcefully($repositoryName, $refSpec);
99
100
        $this->release();
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string[] $commandArguments
107
     *
108
     * @return string[]
109
     */
110
    protected function getCommand(array $commandArguments): array
111
    {
112
        return array_merge(
113
            [
114
                DANDELION_BINARY,
115
                SplitCommand::NAME,
116
            ],
117
            $commandArguments
118
        );
119
    }
120
}
121