Passed
Push — master ( 93e91b...83695f )
by Fabien
02:05
created

SequentialProcessHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Churn\Process\Handler;
6
7
use Churn\Event\Broker;
8
use Churn\Event\Event\AfterFileAnalysisEvent;
9
use Churn\Process\ProcessFactory;
10
use Churn\Process\ProcessInterface;
11
use Churn\Result\Result;
12
use Generator;
13
14
/**
15
 * @internal
16
 */
17
class SequentialProcessHandler extends BaseProcessHandler
18
{
19
20
    /**
21
     * @var Broker
22
     */
23
    private $broker;
24
25
    /**
26
     * @param Broker $broker The event broker.
27
     */
28
    public function __construct(Broker $broker)
29
    {
30
        $this->broker = $broker;
31
    }
32
33
    /**
34
     * Run the processes sequentially to gather information.
35
     *
36
     * @param Generator $filesFinder Collection of files.
37
     * @param ProcessFactory $processFactory Process Factory.
38
     */
39
    public function process(Generator $filesFinder, ProcessFactory $processFactory): void
40
    {
41
        foreach ($filesFinder as $file) {
42
            $result = new Result($file);
43
44
            foreach ($processFactory->createProcesses($file) as $process) {
45
                $this->executeProcess($process, $result);
46
            }
47
48
            $this->broker->notify(new AfterFileAnalysisEvent($result));
49
        }
50
    }
51
52
    /**
53
     * Execute a process and save the result when it's done.
54
     *
55
     * @param ProcessInterface $process The process to execute.
56
     * @param Result $result The result to complete.
57
     */
58
    private function executeProcess(ProcessInterface $process, Result $result): void
59
    {
60
        $process->start();
61
62
        while (!$process->isSuccessful());
63
64
        $this->saveResult($process, $result);
65
    }
66
}
67