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

SequentialProcessHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A executeProcess() 0 7 2
A process() 0 10 3
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