Passed
Pull Request — master (#16)
by Dmitry
14:13
created

ComposerUpdateHook::exec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Composer\Config\Tests\Integration;
6
7
use PHPUnit\Runner\BeforeFirstTestHook;
8
9
final class ComposerUpdateHook implements BeforeFirstTestHook
10
{
11
    public function executeBeforeFirstTest(): void
12
    {
13
        $command = sprintf(
14
            '%s && %s',
15
            $this->cwdToEnvironment(),
16
            '[ -d vendor ] && composer dump || composer upd -n --prefer-dist --no-progress --no-suggest --ignore-platform-reqs ' . $this->suppressLogs(),
17
        );
18
        $this->exec($command);
19
    }
20
21
    private function cwdToEnvironment(): string
22
    {
23
        return sprintf(
24
            'cd %s',
25
            __DIR__ . '/Environment',
26
        );
27
    }
28
29
    private function suppressLogs(): string
30
    {
31
        $commandArguments = $_SERVER['argv'] ?? [];
32
        $isDebug = in_array('--debug', $commandArguments, true);
33
34
        return !$isDebug ? '2>/dev/null' : '';
35
    }
36
37
    private function exec(string $command): void
38
    {
39
        $res = exec($command, $_, $returnCode);
40
        if ((int) $returnCode !== 0) {
41
            throw new \RuntimeException($res);
42
        }
43
    }
44
}
45