Passed
Branch 1.0.x (7d3940)
by Koldo
02:45
created

InstallationPath::ask()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

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