CommandRunner::run()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Scriptura\QuickStart;
6
7
use RuntimeException;
8
use Symfony\Component\Process\Process;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class CommandRunner
12
{
13
    /**
14
     * @var \Symfony\Component\Console\Output\OutputInterface
15
     */
16
    private $output;
17
18
    /**
19
     * @var string
20
     */
21
    private $directory;
22
23
    public function __construct(OutputInterface $output, string $directory)
24
    {
25
        $this->output = $output;
26
        $this->directory = $directory;
27
    }
28
29
    public function run(array $commands): bool
30
    {
31
        $process = Process::fromShellCommandline(implode(' && ', $commands), $this->directory, null, null, null);
32
33
        if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
34
            try {
35
                $process->setTty(true);
36
            } catch (RuntimeException $e) {
37
                $this->output->writeln('Warning: ' . $e->getMessage());
38
            }
39
        }
40
41
        $process->run(function ($type, $line) {
42
            $this->output->write($line);
43
        });
44
45
        return $process->isSuccessful();
46
    }
47
}
48