Completed
Push — master ( 9cfebc...41991d )
by Daniel
03:48
created

Splitter::splitAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Operation\Result\MessageFactoryInterface;
11
use Dandelion\Process\ProcessPoolFactoryInterface;
12
use Dandelion\VersionControl\GitInterface;
13
use Dandelion\VersionControl\SplitshLiteInterface;
14
15
use function sprintf;
16
17
class Splitter extends AbstractOperation
18
{
19
    /**
20
     * @var \Dandelion\VersionControl\GitInterface
21
     */
22
    protected $git;
23
24
    /**
25
     * @var \Dandelion\VersionControl\SplitshLiteInterface
26
     */
27
    protected $splitshLite;
28
29
    /**
30
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
31
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
32
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
33
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
34
     * @param \Dandelion\VersionControl\GitInterface $git
35
     * @param \Dandelion\VersionControl\SplitshLiteInterface $splitshLite
36
     * @param string $binDir
37
     */
38
    public function __construct(
39
        ConfigurationLoaderInterface $configurationLoader,
40
        ProcessPoolFactoryInterface $processPoolFactory,
41
        ResultFactoryInterface $resultFactory,
42
        MessageFactoryInterface $messageFactory,
43
        GitInterface $git,
44
        SplitshLiteInterface $splitshLite,
45
        string $binDir
46
    ) {
47
        parent::__construct($configurationLoader, $processPoolFactory, $resultFactory, $messageFactory, $binDir);
48
49
        $this->git = $git;
50
        $this->splitshLite = $splitshLite;
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(string $repositoryName, string $branch): AbstractOperation
62
    {
63
        $configuration = $this->configurationLoader->load();
64
        $repositories = $configuration->getRepositories();
65
66
        if (!$repositories->offsetExists($repositoryName)) {
67
            throw new RepositoryNotFoundException(\sprintf('Could not find repository "%s".', $repositoryName));
68
        }
69
70
        $repository = $repositories->offsetGet($repositoryName);
71
72
        if (!$this->git->existsRemote($repositoryName)) {
73
            $this->git->addRemote($repositoryName, $repository->getUrl());
74
        }
75
76
        $sha1 = $this->splitshLite->getSha1($repository->getPath());
77
        $refSpec = sprintf('%s:refs/heads/%s', $sha1, $branch);
78
79
        $this->git->pushForcefully($repositoryName, $refSpec);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param string $repositoryName
86
     * @param string $branch
87
     *
88
     * @return string[]
89
     */
90
    protected function getCommand(string $repositoryName, string $branch): array
91
    {
92
        return [
93
            sprintf('%sdandelion', $this->binDir),
94
            SplitCommand::NAME,
95
            $repositoryName,
96
            $branch
97
        ];
98
    }
99
}
100