Passed
Pull Request — master (#3)
by Koldo
02:35
created

InstallationPath::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\Installer\Question;
6
7
use Composer\IO\IOInterface;
8
9
use RuntimeException;
10
11
use function is_dir;
12
use function mkdir;
13
use function sprintf;
14
use function substr;
15
16
class InstallationPath
17
{
18
    private IOInterface $io;
19
    private bool $defaultValue;
20
21 10
    public function __construct(IOInterface $io, bool $defaultValue = true)
22
    {
23 10
        $this->io = $io;
24 10
        $this->defaultValue = $defaultValue;
25 10
    }
26
27 9
    public function ask(string $installationPath): string
28
    {
29
        do {
30 9
            $isValidInstallationPath = $this->io->askConfirmation(
31 9
                sprintf('The application will be installed at "%s" directory [<info>Y</info>/N]: ', $installationPath),
32 9
                $this->defaultValue
33
            );
34 9
            if (false === $isValidInstallationPath) {
35 4
                $installationPath = trim($this->io->ask(
36 4
                    'Add the absolute path to install the project [<info>/opt/app</info>]: ',
37 4
                    '/opt/app'
38
                ));
39
            }
40 9
        } while (false === $isValidInstallationPath);
41
42 9
        return substr($installationPath, -1) === '/'
43 5
            ? substr($installationPath, 0, -1)
44 9
            : $installationPath;
45
    }
46
}
47