Completed
Pull Request — master (#4)
by Jakub
03:36
created

ComposerInstallCommand::import()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Tool\Command;
4
5
use Zalas\Toolbox\Tool\Command;
6
7
final class ComposerInstallCommand implements Command
8
{
9
    private $repository;
10
    private $version;
11
12 9
    public function __construct(string $repository, ?string $version = null)
13
    {
14 9
        $this->repository = $repository;
15 9
        $this->version = $version;
16
    }
17
18 5
    public function __toString(): string
19
    {
20 5
        return \sprintf(
21 5
            'cd /tools && git clone %s && cd /tools/%s && git checkout %s && composer install --no-dev --no-suggest --prefer-dist -n',
22 5
            $this->repository,
23 5
            $this->targetDir(),
24 5
            $this->version ?? '$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null)'
25
        );
26
    }
27
28 5
    private function targetDir(): string
29
    {
30 5
        $targetDir = \preg_replace('#^.*/(.*?)(.git)?$#', '$1', $this->repository);
31
32 5
        return  $targetDir !== $this->repository ? $targetDir : 'tmp';
33
    }
34
}
35