Completed
Pull Request — master (#94)
by Alessandro
08:53
created

Runner::getReturnCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Runner;
5
6
use Paraunit\Filter\Filter;
7
use Paraunit\Lifecycle\EngineEvent;
8
use Paraunit\Lifecycle\ProcessEvent;
9
use Paraunit\Process\ProcessBuilderFactory;
10
use Paraunit\Process\SymfonyProcessWrapper;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Class Runner
16
 * @package Paraunit\Runner
17
 */
18
class Runner implements EventSubscriberInterface
19
{
20
    /** @var  ProcessBuilderFactory */
21
    private $processBuilderFactory;
22
23
    /** @var EventDispatcherInterface */
24
    private $eventDispatcher;
25
26
    /** @var Filter */
27
    private $filter;
28
29
    /** @var PipelineCollection */
30
    private $pipelineCollection;
31
32
    /** @var \SplQueue */
33
    private $queuedProcesses;
34
35
    /** @var int */
36
    private $exitCode;
37
38
    /**
39
     * @param EventDispatcherInterface $eventDispatcher
40
     * @param ProcessBuilderFactory $processFactory
41
     * @param Filter $filter
42
     * @param PipelineCollection $pipelineCollection
43
     */
44
    public function __construct(
45
        EventDispatcherInterface $eventDispatcher,
46
        ProcessBuilderFactory $processFactory,
47
        Filter $filter,
48 15
        PipelineCollection $pipelineCollection
49
    ) {
50
        $this->eventDispatcher = $eventDispatcher;
51
        $this->processBuilderFactory = $processFactory;
52
        $this->filter = $filter;
53
        $this->pipelineCollection = $pipelineCollection;
54
        $this->queuedProcesses = new \SplQueue();
55 15
        $this->exitCode = 0;
56 15
    }
57 15
58 15
    public static function getSubscribedEvents(): array
59
    {
60 15
        return [
61 15
            ProcessEvent::PROCESS_TERMINATED => 'pushToPipeline',
62 15
            ProcessEvent::PROCESS_PARSING_COMPLETED => 'onProcessParsingCompleted',
63 15
        ];
64
    }
65
66
    /**
67
     * @return int The final exit code: 0 if no failures, 10 otherwise
68
     */
69
    public function run(): int
70 15
    {
71
        $this->eventDispatcher->dispatch(EngineEvent::BEFORE_START);
72 15
73 15
        $this->createProcessQueue();
74
75 15
        $this->eventDispatcher->dispatch(EngineEvent::START);
76 15
77
        do {
78 15
            $this->pushToPipeline();
79 15
            usleep(100);
80 15
            $this->pipelineCollection->triggerProcessTermination();
81
        } while ($this->pipelineCollection->hasRunningProcesses() || ! $this->queuedProcesses->isEmpty());
82
83 15
        $this->eventDispatcher->dispatch(EngineEvent::END);
84 15
85 15
        return $this->exitCode;
86
    }
87
88 15
    /**
89 15
     * @param ProcessEvent $processEvent
90 15
     */
91 15
    public function onProcessParsingCompleted(ProcessEvent $processEvent)
92 15
    {
93
        $process = $processEvent->getProcess();
94
95 15
        if ($process->isToBeRetried()) {
96
            $process->reset();
97
            $process->increaseRetryCount();
98 15
99
            $this->queuedProcesses->enqueue($process);
100
101
            $this->eventDispatcher->dispatch(ProcessEvent::PROCESS_TO_BE_RETRIED, new ProcessEvent($process));
102 15
        } elseif ($process->getExitCode() !== 0) {
103
            $this->exitCode = 10;
104 15
        }
105 15
    }
106 15
107
    private function createProcessQueue()
108 15
    {
109
        foreach ($this->filter->filterTestFiles() as $file) {
110
            $processBuilder = $this->processBuilderFactory->create($file);
111
            $this->queuedProcesses->enqueue(new SymfonyProcessWrapper($processBuilder, $file));
112 15
        }
113
    }
114
115
    public function pushToPipeline()
116
    {
117
        while (! $this->queuedProcesses->isEmpty() && $this->pipelineCollection->hasEmptySlots()) {
118 15
            $this->pipelineCollection->push($this->queuedProcesses->dequeue());
119
        }
120 15
    }
121
}
122