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

StreamedCommandResponseGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 2
A runJSON() 0 5 1
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