Total Complexity | 6 |
Total Lines | 33 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 update -n --prefer-dist --no-progress --ignore-platform-reqs --no-plugins ' . $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 | } |
||
46 |