Passed
Pull Request — master (#9)
by John
02:59
created

Initializer::addGitRemotes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 26
rs 9.7
1
<?php
2
3
namespace Dandelion\Operation;
4
5
use Dandelion\Configuration\ConfigurationLoaderInterface;
6
use Dandelion\Operation\Result\MessageFactoryInterface;
7
use Dandelion\Operation\Result\MessageInterface;
8
use Dandelion\VersionControl\GitInterface;
9
10
class Initializer implements InitializerInterface
11
{
12
    /**
13
     * @var \Dandelion\VersionControl\GitInterface
14
     */
15
    private $git;
16
    /**
17
     * @var \Dandelion\Configuration\ConfigurationLoaderInterface
18
     */
19
    private $configurationLoader;
20
    /**
21
     * @var \Dandelion\Operation\Result\MessageFactoryInterface
22
     */
23
    private $messageFactory;
24
    /**
25
     * @var \Dandelion\Operation\ResultFactoryInterface
26
     */
27
    private $resultFactory;
28
29
    /**
30
     * @param \Dandelion\VersionControl\GitInterface $git
31
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
32
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
33
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
34
     */
35
    public function __construct(
36
        GitInterface $git,
37
        ConfigurationLoaderInterface $configurationLoader,
38
        MessageFactoryInterface $messageFactory,
39
        ResultFactoryInterface $resultFactory
40
    ) {
41
        $this->git = $git;
42
        $this->configurationLoader = $configurationLoader;
43
        $this->messageFactory = $messageFactory;
44
        $this->resultFactory = $resultFactory;
45
    }
46
47
    /**
48
     * @return \Dandelion\Operation\ResultInterface
49
     */
50
    public function addGitRemotes(): ResultInterface
51
    {
52
        $configuration = $this->configurationLoader->load();
53
        $repositories = $configuration->getRepositories();
54
        $result = $this->resultFactory->create();
55
56
        foreach ($repositories as $repositoryName => $repository) {
57
            if ($this->git->existsRemote($repositoryName)) {
58
                $message = $this->messageFactory->create()
59
                    ->setType(MessageInterface::TYPE_INFO)
60
                    ->setText(sprintf('%s already exists, skip.', $repositoryName));
61
62
                $result->addMessage($message);
63
                continue;
64
            }
65
66
            $success = $this->git->addRemote($repositoryName, $repository->getUrl());
67
            $type = $success ? MessageInterface::TYPE_INFO : MessageInterface::TYPE_ERROR;
68
            $message = $this->messageFactory->create()
69
                ->setType($type)
70
                ->setText($repositoryName);
71
72
            $result->addMessage($message);
73
        }
74
75
        return $result;
76
    }
77
}
78