Passed
Pull Request — master (#53)
by Alexander
13:52
created

ComposerUpdateHook::cwdToEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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