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

AbstractOperation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A executeForAllRepositories() 0 16 2
A getCallback() 0 13 2
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 Symfony\Component\Process\Process;
13
14
abstract class AbstractOperation
15
{
16
    /**
17
     * @var \Dandelion\Configuration\ConfigurationLoaderInterface
18
     */
19
    protected $configurationLoader;
20
21
    /**
22
     * @var \Dandelion\Process\ProcessPoolFactoryInterface
23
     */
24
    protected $processPoolFactory;
25
26
    /**
27
     * @var \Dandelion\Operation\ResultFactoryInterface
28
     */
29
    protected $resultFactory;
30
31
    /**
32
     * @var \Dandelion\Operation\Result\MessageFactoryInterface
33
     */
34
    protected $messageFactory;
35
36
    /**
37
     * @var string
38
     */
39
    protected $binDir;
40
41
    /**
42
     * @param \Dandelion\Configuration\ConfigurationLoaderInterface $configurationLoader
43
     * @param \Dandelion\Process\ProcessPoolFactoryInterface $processPoolFactory
44
     * @param \Dandelion\Operation\ResultFactoryInterface $resultFactory
45
     * @param \Dandelion\Operation\Result\MessageFactoryInterface $messageFactory
46
     * @param string $binDir
47
     */
48
    public function __construct(
49
        ConfigurationLoaderInterface $configurationLoader,
50
        ProcessPoolFactoryInterface $processPoolFactory,
51
        ResultFactoryInterface $resultFactory,
52
        MessageFactoryInterface $messageFactory,
53
        string $binDir
54
    ) {
55
        $this->configurationLoader = $configurationLoader;
56
        $this->processPoolFactory = $processPoolFactory;
57
        $this->resultFactory = $resultFactory;
58
        $this->messageFactory = $messageFactory;
59
        $this->binDir = $binDir;
60
    }
61
62
    /**
63
     * @param string $repositoryName
64
     * @param string $branch
65
     *
66
     * @return \Dandelion\Operation\AbstractOperation
67
     */
68
    abstract public function executeForSingleRepository(string $repositoryName, string $branch): AbstractOperation;
69
70
    /**
71
     * @param string $branch
72
     *
73
     * @return \Dandelion\Operation\ResultInterface
74
     */
75
    public function executeForAllRepositories(string $branch = 'master'): ResultInterface
76
    {
77
        $configuration = $this->configurationLoader->load();
78
        $processPool = $this->processPoolFactory->create();
79
        $result = $this->resultFactory->create();
80
81
        foreach ($configuration->getRepositories() as $repositoryName => $repository) {
82
            $processPool->addProcess(
83
                $this->getCommand($repositoryName, $branch),
84
                $this->getCallback($result, $repositoryName)
85
            );
86
        }
87
88
        $processPool->start();
89
90
        return $result;
91
    }
92
93
    /**
94
     * @param \Dandelion\Operation\ResultInterface $result
95
     * @param string $repositoryName
96
     *
97
     * @return \Closure
98
     */
99
    protected function getCallback(ResultInterface $result, string $repositoryName): Closure
100
    {
101
        $messageFactory = $this->messageFactory;
102
103
        return static function (Process $process) use ($result, $messageFactory, $repositoryName) {
104
            // @codeCoverageIgnoreStart
105
            $type = $process->isSuccessful() ? MessageInterface::TYPE_INFO : MessageInterface::TYPE_ERROR;
106
107
            $message = $messageFactory->create()
108
                ->setType($type)
109
                ->setText($repositoryName);
110
111
            $result->addMessage($message);
112
            // @codeCoverageIgnoreEnd
113
        };
114
    }
115
116
    /**
117
     * @param string $repositoryName
118
     * @param string $branch
119
     *
120
     * @return string[]
121
     */
122
    abstract protected function getCommand(string $repositoryName, string $branch): array;
123
}
124