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

InstallationPath   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A ask() 0 18 4
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