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

InstallationPath   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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