AbstractOperation::getCallback()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 19
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dandelion\Operation;
6
7
use Closure;
8
use Dandelion\Configuration\ConfigurationLoaderInterface;
9
use Dandelion\Operation\Result\MessageFactoryInterface;
10
use Dandelion\Operation\Result\MessageInterface;
11
use Dandelion\Process\ProcessPoolFactoryInterface;
12
use Dandelion\VersionControl\Platform\PlatformFactoryInterface;
13
use Symfony\Component\Process\Process;
14
15
use function array_merge;
16
use function sprintf;
17
18
abstract class AbstractOperation
19
{
20
    /**
21
     * @var \Dandelion\Configuration\ConfigurationLoaderInterface
22
     */
23
    protected $configurationLoader;
24
25
    /**
26
     * @var \Dandelion\Process\ProcessPoolFactoryInterface
27
     */
28
    protected $processPoolFactory;
29
30
    /**
31
     * @var \Dandelion\Operation\ResultFactoryInterface
32
     */
33
    protected $resultFactory;
34
35
    /**
36
     * @var \Dandelion\Operation\Result\MessageFactoryInterface
37
     */
38
    protected $messageFactory;
39
40
    /**
41
     * @var \Dandelion\VersionControl\Platform\PlatformFactoryInterface
42
     */
43
    protected $platformFactory;
44
45
    /**
46
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
47
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
48
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
49
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
50
     * @param \Dandelion\VersionControl\Platform\PlatformFactoryInterface $platformFactory
51
     */
52
    public function __construct(
53
        ConfigurationLoaderInterface $configurationLoader,
54
        ProcessPoolFactoryInterface $processPoolFactory,
55
        ResultFactoryInterface $resultFactory,
56
        MessageFactoryInterface $messageFactory,
57
        PlatformFactoryInterface $platformFactory
58
    ) {
59
        $this->configurationLoader = $configurationLoader;
60
        $this->processPoolFactory = $processPoolFactory;
61
        $this->resultFactory = $resultFactory;
62
        $this->messageFactory = $messageFactory;
63
        $this->platformFactory = $platformFactory;
64
    }
65
66
    /**
67
     * @param string[] $arguments
68
     *
69
     * @return \Dandelion\Operation\ResultInterface
70
     */
71
    public function executeForAllRepositories(array $arguments = []): ResultInterface
72
    {
73
        $configuration = $this->configurationLoader->load();
74
        $processPool = $this->processPoolFactory->create();
75
        $result = $this->resultFactory->create();
76
77
        foreach ($configuration->getRepositories() as $repositoryName => $repository) {
78
            $commandArguments = array_merge([$repositoryName], $arguments);
79
80
            $processPool->addProcess(
81
                $this->getCommand($commandArguments),
82
                $this->getCallback($result, $repositoryName)
83
            );
84
        }
85
86
        $processPool->start();
87
88
        return $result;
89
    }
90
91
    /**
92
     * @param \Dandelion\Operation\ResultInterface $result
93
     * @param string $repositoryName
94
     *
95
     * @return \Closure
96
     */
97
    protected function getCallback(ResultInterface $result, string $repositoryName): Closure
98
    {
99
        $messageFactory = $this->messageFactory;
100
101
        return static function (Process $process) use ($result, $messageFactory, $repositoryName) {
102
            // @codeCoverageIgnoreStart
103
            $type = MessageInterface::TYPE_INFO;
104
            $text = $repositoryName;
105
106
            if (!$process->isSuccessful()) {
107
                $type = MessageInterface::TYPE_ERROR;
108
                $text = sprintf('%s -> %s', $repositoryName, $process->getErrorOutput());
109
            }
110
111
            $message = $messageFactory->create()
112
                ->setType($type)
113
                ->setText($text);
114
115
            $result->addMessage($message);
116
            // @codeCoverageIgnoreEnd
117
        };
118
    }
119
120
    /**
121
     * @param string[] $commandArguments
122
     *
123
     * @return string[]
124
     */
125
    abstract protected function getCommand(array $commandArguments): array;
126
}
127