HalfAsynchronousProcessRunner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * File:HalfAsynchronousProcessRunner.php
6
 *
7
 * @author Maciej Sławik <[email protected]>
8
 * @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl)
9
 */
10
11
12
namespace MSlwk\ReactPhpPlayground\Standalone\ChildProcess;
13
14
use MSlwk\ReactPhpPlayground\Api\Data\ProcessInterface;
15
use MSlwk\ReactPhpPlayground\Model\Adapter\ReactPHP\ProcessFactory;
16
use MSlwk\TypeSafeArray\ObjectArray;
17
use React\EventLoop\LoopInterface;
18
19
/**
20
 * Class HalfAsynchronousProcessRunner
21
 * @package MSlwk\ReactPhpPlayground\Standalone\ChildProcess
22
 */
23
class HalfAsynchronousProcessRunner implements ProcessRunnerInterface
24
{
25
    /**
26
     * @var LoopInterface
27
     */
28
    private $loop;
29
30
    /**
31
     * @var ProcessFactory
32
     */
33
    private $processFactory;
34
35
    /**
36
     * AsynchronousProcessRunner constructor.
37
     * @param LoopInterface $loop
38
     * @param ProcessFactory $processFactory
39
     */
40
    public function __construct(
41
        LoopInterface $loop,
42
        ProcessFactory $processFactory
43
    ) {
44
        $this->loop = $loop;
45
        $this->processFactory = $processFactory;
46
    }
47
48
    /**
49
     * @param ObjectArray $processes
50
     * @return void
51
     */
52
    public function runProcesses(ObjectArray $processes): void
53
    {
54
        for ($i = 0; $i < ($processes->count() / 2); $i++) {
55
            $this->createProcessDefinition($processes->offsetGet($i));
56
        }
57
        $this->loop->run();
58
59
        for ($i; $i < ($processes->count()); $i++) {
60
            $this->createProcessDefinition($processes->offsetGet($i));
61
        }
62
        $this->loop->run();
63
    }
64
65
    /**
66
     * @codeCoverageIgnore
67
     * @param ProcessInterface $process
68
     * @return void
69
     */
70 View Code Duplication
    protected function createProcessDefinition(ProcessInterface $process): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $reactProcess = $this->processFactory->create($process->getCommand());
73
        $reactProcess->start($this->loop);
74
75
        $reactProcess->stdout->on('data', function ($chunk) {
76
            echo $chunk;
77
        });
78
    }
79
}
80