Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WorkingDirectorySwitcher
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 1
 switchTo
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\SelfUpdate;
21
22use InvalidArgumentException;
23
24use function Safe\chdir;
25use function Safe\realpath;
26
27/**
28 * Applies Composer-like working-directory switching for the standalone binary.
29 */
30 class WorkingDirectorySwitcher implements WorkingDirectorySwitcherInterface
31{
32    /**
33     * Switches to the provided working directory when one is configured.
34     *
35     * @param string|null $workingDirectory the target working directory, or null when no switch is requested
36     */
37    public function switchTo(?string $workingDirectory): void
38    {
39        if (null === $workingDirectory || '' === $workingDirectory) {
40            return;
41        }
42
43        if (! is_dir($workingDirectory)) {
44            throw new InvalidArgumentException(\sprintf(
45                'The working directory "%s" does not exist.',
46                $workingDirectory
47            ));
48        }
49
50        chdir(realpath($workingDirectory));
51    }
52}