Passed
Push — 6.5.0.0 ( 85bd4e...fe6596 )
by Christian
24:46 queued 09:10
created

StreamedCommandResponseGenerator::run()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 16
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Services;
5
6
use Shopware\Core\Framework\Log\Package;
7
use Symfony\Component\HttpFoundation\StreamedResponse;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * @internal
12
 */
13
#[Package('core')]
14
class StreamedCommandResponseGenerator
15
{
16
    /**
17
     * @param array<string> $params
18
     * @param callable(Process): void $finish
19
     */
20
    public function run(array $params, callable $finish): StreamedResponse
21
    {
22
        $process = new Process($params);
23
        $process->setEnv(['COMPOSER_HOME' => sys_get_temp_dir() . '/composer']);
24
        $process->setTimeout(300);
25
26
        $process->start();
27
28
        return new StreamedResponse(function () use ($process, $finish): void {
29
            foreach ($process->getIterator() as $item) {
30
                \assert(\is_string($item));
31
                echo $item;
32
                flush();
33
            }
34
35
            $finish($process);
36
        });
37
    }
38
39
    /**
40
     * @param array<string> $params
41
     */
42
    public function runJSON(array $params): StreamedResponse
43
    {
44
        return $this->run($params, function (Process $process): void {
45
            echo json_encode([
46
                'success' => $process->isSuccessful(),
47
            ]);
48
        });
49
    }
50
}
51