SequentialProcessHandler::executeProcess()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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