CommandRunner   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 17 5
A __construct() 0 4 1
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